Scala does not permit to create laze vars, only lazy vals. It make sense.
But I\'ve bumped on use case, where I\'d like to have similar capability. I need a lazy var
If you want to keep on using a lazy val
(it can be used in path-dependent types and it's thread safe), you can add a layer of indirection in its definition (previous solutions use var
s as an indirection):
lazy val value: Int = thunk()
@volatile private var thunk: () => Int = ..
thunk = ...
thunk = ...
You could encapsulate this in a class if you wanted to reuse it, of course.