title: Avoiding NPE in trait initialization without using lazy vals
body: <p>This is probably covered by the <a href="http://daily-scala.blogspot.co.at/2010/03/nullpointer-when-mixed-traits-warning.html" rel="nofollow">blog entry</a> by Jesse Eichar—still I can't figure out how to correct the following <strong>without resorting to lazy vals</strong> so that the NPE is fixed:</p>
  
  <p>Given</p>
  
  <pre><code>trait FooLike { def foo: String }
  case class Foo(foo: String) extends FooLike
  
  trait Sys {
    type D &lt;: FooLike
    def bar: D
  }
  
  trait Confluent extends Sys {
    type D = Foo
  }
  
  trait Mixin extends Sys {
    val global = bar.foo
  }
  </code></pre>
  
  <p>First attempt:</p>
  
  <pre><code>class System1 extends Mixin with Confluent {
    val bar = Foo("npe")
  }
  
  new System1  // boom!!
  </code></pre>
  
  <p>Second attempt, changing mixin order</p>
  
  <pre><code>class System2 extends Confluent with Mixin {
    val bar = Foo("npe")
  }
  
  new System2  // boom!!
  </code></pre>
  
  <p>Now I use both <code>bar</code> and <code>global</code> 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?</p>