Jon Steelman | 20 May 2012 16:11
Picon

Should a self-referential val/var declaration really compile?

(Sending to scala-language per Naftoli)

Should the below really compile?

Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_31).
Type in expressions to have them evaluated.
Type :help for more information.

scala> var s: String = s
s: String = null


Same behavior in 2.9.2 and 2.10.0-M3

Thanks,
Jon


Mark Grand added:

I think you have found a hole in the language spec.  The language
reference talks about scope, but does not define precise lexical rules
for where scopes begin.  So it appears that the scope begins somewhere
to the left of the equals sign.  Here are some other things that
compile:

scala> var j : Int = j
j: Int = 0

scala> var b: Boolean = b
b: Boolean = false

scala> var v:Int = v+1
v: Int = 1

scala> val x:Int = x
x: Int = 0

scala> lazy val d:Int = d+1
d: Int = <lazy>

scala> d
java.lang.StackOverflowError


Gmane