I got the following code snippet referring an example in Chapter 6 of "Programming Scala":
object HelloWorld {
def main(args: Array[String]) {
trait AbstractT2 {
println("In AbstractT2:")
val value: Int
val inverse = 1.0 / value // ???
println("AbstractT2: value = " + value + ", inverse = " + inverse)
}
val c2b = new AbstractT2 {
println("In c2b:") //---->line 1
val value = 10 //---->line 2
}
println("c2b.value = " + c2b.value + ", inverse = " + c2b.inverse)
}
}
The result of the above code is:
In AbstractT2:
AbstractT2: value = 0, inverse = Infinity
In c2b:
c2b.value = 10, inverse = Infinity
Since the anonymous class initialization is after the trait initialization, the result is understandable. But if I exchange line 1 and line 2 in the above example, so that val value = 10
precedes println("In c2b:")
, the result will be:
In AbstractT2:
AbstractT2: value = 10, inverse = 0.1
In c2b:
c2b.value = 10, inverse = 0.1
It seems this time the initialization is successful although it's wrong from language point of view. I can't understand why. Can anybody help on this? Thanks a lot.