If I write :
trait T {
val t = 3
val u = 1::t::Nil
}
class U extends T {
override val t = 2
}
(new U).u
it shows this.
List(1, 0)
How should I change the above code to make it display the following:
List(1, 2)
i.e. override val t
sets the value of t
for u
in the trait T
?
Try using an early initializer:
See e.g. here for more information about early initialization.
All scala declarative style is just an illusion. Scala is build upon a jvm and works like java.
Evetything is a class and should be independent on its usage (java is not c++ and supports incremental build with its pros and cons). Every trait has its own initialization code and multi-trait class runs respective initialization code one by one. If you use some AnyRef that is declared only in a subclass than that its value will be set for null during initialization.
I guard myself with specifing convention rule: every val should be either final or lazy (why using plain val in non-final classes) . So I don't care about initialization order and may pretend further that I'm using declarative language.
Also I'm using option
-Xcheckinit
: Add runtime check to field accessors.One way to do this is to delay evaluation of
u
by usingdef
orlazy val
as follows:or
The differences are as follows:
val
makes an expression evaluate during initializationdef
makes an expression evaluate each timeu
is usedlazy val
makes it evaluated on firstu
usage and caches the result