Created
May 2, 2012 13:58
-
-
Save Rogach/2576699 to your computer and use it in GitHub Desktop.
after init article
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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