我已经试了:
class Foo(bar: Int)
Run Code Online (Sandbox Code Playgroud)
VS:
class Foo(private val bar: Int)
Run Code Online (Sandbox Code Playgroud)
而且他们似乎表现得一样,虽然我找不到任何地方说(bar:
Int)扩大到(private val bar: Int)所以我的问题是,这些相同/相似吗?
在旁注中,我一直在尝试使用-Xprint:typer这些代码片段,除了第二行中的额外行之外,它们产生相同的代码.我该如何阅读额外的行?
..
class Foo extends scala.AnyRef {
<paramaccessor> private[this] val bar: Int = _;
def <init>(bar: Int): this.Foo = {
Foo.super.<init>();
()
}
}
..
..
class Foo extends scala.AnyRef {
<paramaccessor> private[this] val bar: Int = _;
<stable> <accessor> <paramaccessor> private def bar: Int = Foo.this.bar;
def <init>(bar: Int): this.Foo = {
Foo.super.<init>();
()
}
}
..
Run Code Online (Sandbox Code Playgroud)