Executing code on creation in scala trait subclass -
i'd define val in trait gets computed once sub-class gets instantiated.
for instance :
trait example { val p : int val after : int = p + 1 } class ex(x : int) extends example { val p = x } i want after computed each instance of ex, no matter parameter x give, after 1. if, p 0 computation.
of course, works def, no longer computed once.
after in example instantiated right away, before ex instantiated - , before looking @ x.
to fix can turn after lazy val, evaluated first time use it.
Comments
Post a Comment