I assume there are good reasons for not doing that, but I can't think
of them off hand.
--Rex
--
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/groups/opt_out.
The "easiest" and also very annoying way to ensure your safety is to compile twice:(1) with -Xcheckinit(2) with -optimise (or just not -Xcheckinit)Checkinit will emit runtime errors if you attempt to access something which is not initialized yet.
I don't think any build tool gives you a workflow for compiling your program with different byte-code outputs though, but it's essentially what you want.
Yeah, the problem is it's possible to define it so that it works:scala> class Foo {| val bar = baz| val baz = "O NOES"| }defined class Fooscala> new { override val baz = "O NOES" } with Foores1: Foo = $anon$1@133b32e5scala> res1.barres2: String = O NOESThey're called "Early Initializers", and they help solve the startup issue of classes. We allow traits to have vals, and also allow this kind of ambiguity because sometimes it's handy.
-jason
Hello,
This seems not be confined to fields and class declarations:
scala> println(x); val x = "Oops"
null
x: String = Oops
They're called "Early Initializers", and they help solve the startup issue of classes. We allow traits to have vals, and also allow this kind of ambiguity because sometimes it's handy.However, many times we really do want a more principled solution. -Xcheckinit will at least give you a useful runtime warning telling you which vals were not initialized so you can use Early-Initializers. It's not a perfect solution, but it's better than nothing.- Josh
I'm not saying it shouldn't, just ape-ing justification I've heard before.
The enemy of "this one little thing" is "universal applicability".
I'd be all for seeing a compiler warning, promotable to error when we see this. Just stating that it is possible for this code to eventually work. Unless the class final/sealed, we can't know for certain (but we can make a pretty darn good guess).
--
Yes as indicated it shouldn't apply to abstract classes.