Avoiding NPE in trait initialization without using lazy vals
- by 0__
This is probably covered by the blog entry by Jesse Eichar—still I can't figure out how to correct the following without residing to lazy vals so that the NPE is fixed:
Given
trait FooLike { def foo: String }
case class Foo( foo: String ) extends FooLike
trait Sys {
type D <: FooLike
def bar: D
}
trait Confluent extends Sys {
type D = Foo
}
trait Mixin extends Sys {
val global = bar.foo
}
First attempt:
class System1 extends Mixin with Confluent {
val bar = Foo( "npe" )
}
new System1 // boom!!
Second attempt, changing mixin order
class System2 extends Confluent with Mixin {
val bar = Foo( "npe" )
}
new System2 // boom!!
Now I use both bar and global very heavily, and therefore I don't want to pay a lazy-val tax just because Scala (2.9.2) doesn't get the initialisation right. What to do?