Please note that this question and the like were asked earlier, for example, in Forward links - why is this code compiled? but I found answers that still remain some questions open, so I have another problem.
Inside the methods and functions, the effect of the keyword valseems lexical, i.e.
def foo {
println(bar)
val bar = 42
}
getting
error: forward reference extends over definition of value bar
However, in classes, visibility rules valseem to change:
object Foo {
def foo = bar
println(bar)
val bar = 42
}
This compilation is not only compiled, but also printlnin the constructor will give 0as output, and a call fooafter the complete construction of the instance will lead to the expected value 42.
, , forward-reference, , , (, , ), , , , .
:
, - , , val , .
. .
, val , val, , lazy.
, , val lazy, val , , , , .
, - , val -alike, -, , , ?