Q:

Avoiding NPE in trait initialization without using lazy vals

This is probably covered by the blog entry by Jesse Eichar—still I can't figure out how to correct the following without resorting 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?

A:

You can use an early initializer:
class System1 extends {
  val bar = Foo("npe")
} with Mixin with Confluent {
  // ...
}

scala> new System1
res3: System1 = System1@1d0bfedd

