Possible Duplicate:
Forward References - why does this code compile?
Scala and forward references
object Main extends App {
val a = 4
val b = a + c
val c = 5
println(b) // => 4
}
This will print 4 as c
apparently is 0
when b
is assigned. a
and c
are values, so they should not be 0
in one moment, and 5
in the next. In Scala, they should be immutable, right?
Shouldn't I at least get some sort of warning? Of course, in the example above, you have to be an idiot to miss that, but in more complicated cases, it can be hard to tell what order to put it.
I am of course aware that I can use lazy val b = ...
but what if I think I've put it in the right order, when I really haven't. Isn't it dangerous as hell not getting any warning at all on this? This runs just fine!? How does this get passed both Scala IDE and the compiler? Is it on purpose? Or is it a bug that cannot be fixed? :/
:)