Closed
Description
From scala-language
On Tuesday, March 22, 2011 at 5:14:16 PM UTC+2, Eugen Labun wrote:
Hi all,
eventually (or even surely) this is an old question but I haven't found an explanation for this
simple situation (nor in Language Specification, nor in Scala-Book).
(More complicated cases are described in this excellent faq from Paul:
https://github.com/paulp/scala-faq/wiki/Initialization-Order)
Constructions like
def m {
val x = y // compile error "forward reference extends over definition of value x"
val y = 5
}
cause a compile error if used in methods,
but are accepted in constructors/initializers:
class/object T {
val x = y // compiles OK, x = 0
val y = 5
}
The 'x' in the code above gets initialized to 0 (!), not to 5.
Both -- acceptance by the compiler and initialization to 0 -- are counterintuitive to me.
Would a compile error for the second case ("illegal forward reference") not be a more preferable?
Metadata
Metadata
Assignees
Labels
No labels