If you are using Scala 2.9, you can arrange something like this:
class A { println("Hi") } class B extends A with DelayedInit { private[this] var count = 0 println("Hey") def delayedInit(x: => Unit) { x count += 1 if (count==2) { println("There") } } } class C extends B { println("Ho") } class D extends C { println("Ha") }
This uses the new DelayedInit attribute, which sends the deferred constructors from the current and all child classes to the DelayedInit method. Unfortunately, since there is no completion signal, you are limited to skipping one constructor. So, for C we get:
scala> new C Hi Hey Ho There
where the block "There" magically appeared after the block "Ho" from C Unfortunately, if you extend C , the new initialization happens last:
scala> new D Hi Hey Ho There Ha
(You really don't need A there ... I just put it there to illustrate what happens to superclasses.)
source share