Dear all,
I have a base trait
trait T1 {val v: Int; require(v > 0)}
which needs to enforce that v > 0
to all classes deriving from it. The scheme above, however, doesn’t work because v
is already initialised (to 0
) when the overridden value for v
is provided:
scala> val t1 = new T1{val v = 14}
java.lang.IllegalArgumentException: requirement failed
at scala.Predef$.require(Predef.scala:212)
at T1$class.$init$(<console>:14)
... 33 elided
Choosing v
to be lazy
doesn’t help either because, then, it needs to be initialised but no default value makes sense for v
in the context of my work. How would I go about enforcing the require
ment then?
There is a reason, in fact, why I don’t make T1
a class. That’s because, my “real” T1
also has abstract functions and passing those when extend
ing T1
is such a disgust.
TIA,
–Hossein