fill takes a call by name parameter, like Future, so it would be safe if it didn't call it until later (or never). The compiler doesn't know when it will be called.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Well, it doesn't do what I meant. And I expect my future Scala students to make this mistake and getting rather confused... So I'd sure would like to get help from the compiler here, so that the mistake in the "nowarning"-object can be fixed by the human e.g. by turning "many" into a lazy val or change the order of declarations.
scala> object nowarning {val many = Array.fill(10)(one); val one = 1}
defined object nowarning
scala> nowarning.many
res0: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Another similar mistake is indeed caught by the compiler, when it is even refusing to compile:
scala> {val many = Array.fill(10)(one); val one = 1}
<console>:12: error: forward reference extends over definition of value many
{val many = Array.fill(10)(one); val one = 1}
^
The difference in behavior between members and local variables is easy to miss...
http://stackoverflow.com/questions/7762838/forward-references-why-does-this-code-compile
Unfortunately the difference between locals and members is necessary, since initialization order of locals is much more deterministic.