In the following statement the val f
is defined as a lambda that references itself (it is recursive):
val f: Int => Int = (a: Int) =>
if (a > 10) 3 else f(a + 1) + 1 // just some simple function
I've tried it in the REPL, and it compiles and executes correctly.
According to the specification, this seems like an instance of illegal forward referencing:
In a statement sequence
s[1]...s[n]
making up a block, if a simple name ins[i]
refers to an entity defined bys[j]
wherej >= i
, then for alls[k]
between and includings[i]
ands[j]
,
s[k]
cannot be a variable definition.- If
s[k]
is a value definition, it must belazy
.
The assignment is a single statement, so it satisfied the j >= i
criteria, and it is included in the interval of statements the two rules apply to (between and including s[i]
and s[j]
).
However, it seems that it violates the second rule, because f
is not lazy.
How is that a legal statement (tried it in Scala 2.9.2)?