Yesterday I was reading a paper called: How Scala Experience Improved Our Java Development.
Inside the Object Initialization section, it says:
The idiomatic Scala technique for instantiating an object (which does not expose all relevant parameters via constructor arguments) is to create an anonymous subclass with an initializer block which calls additional statements [Odersky et al.]:
I have been doing some tests:
class B {
var x1: String = "B"
def setText(text: String) {
x1 = text
}
override def toString = x1
}
I don't really understand why I can do:
scala> new B{setText("new Text")}
res23: B = new Text
but I can't do:
scala> new B{ setText "new Text" }
<console>:1: error: ';' expected but string literal found.
new B{ setText "new Text" }
^