Or are vals in scala objects lazy by default?
Anyway, if it’s necessary to declare a val in an object lazy by using lazy, is it possible to do something like
lazy object SomeObject
or (like you do in c++)
object A {
lazy:
val a
val b
...
}
Because I would like to be lazy and not have to relabel all my vals lazy val
To answer your first question (“are
vals in scala objects lazy by default?”): No, not exactly, but the objects themselves are kind of lazy, which may be lazy enough. From 5.4 (“Object Definitions”) of the Scala language specification:So, for example, if we have these three objects:
Then when we run
Z, we see the following:So the
valinXisn’t lazy, but it’s also not evaluated until the first time we useX.