I need to execute some code after the subclass already did all it’s initialization, for example:
abstract class A(a:String) {
var sum = 0
def add(n:Int) = { sum += n; sum }
def verify = if (sum > 10) () else throw new Exception
... initialize subclass ...
verify
}
class B extends A("In A") {
val smth = add(50)
// I want to avoid calling `verify` here
}
val b = new B
println(b.smth) // 50
Is there a way to do it?
So it seems I found the answer. I decided to go with the DelayedInit trait approach – I just execute the delayed code (and count number of times it was executed), then execute the needed code when I think that I’ve seen enough initializations (one for each class in the extension hierarchy). I wrapped it into a trait:
Usage: