Skip to content

Instantly share code, notes, and snippets.

@Rogach
Created May 2, 2012 13:58
Show Gist options
  • Save Rogach/2576699 to your computer and use it in GitHub Desktop.
Save Rogach/2576699 to your computer and use it in GitHub Desktop.
after init article
trait AfterInit extends DelayedInit {
def afterInit
def afterInitLevel = 1
private var initCount = 0
private def getInitNumber(clazz: Class[_]):Int =
if (clazz.getSuperclass == classOf[java.lang.Object]) 0 else getInitNumber(clazz.getSuperclass) + 1
final def delayedInit(x: => Unit) {
x
initCount += 1
if (getInitNumber(this.getClass) + afterInitLevel == initCount) afterInit
}
}
abstract class A(id:String) extends AfterInit {
def afterInit = println("Inited!")
}
class B extends A("BB") {
println("B inited!")
}
new B // prints "B inited!", "Inited!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment