For instance, in a toString
method, I would like to give info on whether a lazy val member of a class has been evaluated, and if so, print its value. Is this possible?
As far as I know, you can´t. But you can help with that:
class A {
var isMemberSet = false
lazy val member = { isMemberSet = true; 8 }
}
val a = new A
a.isMemberSet // false
a.member // 8
a.isMemberSet // true
Of course, visibility and access modifier have to be adapted.
If you want direct access to the compiler generated field, please try the following code.
import java.lang.reflect._
class A {
lazy val member = 42
def isEvaluated =
(1 & getClass.getField("bitmap$0").get(this).asInstanceOf[Int]) == 1
}
val a = new A
println(a.isEvaluated) // => true
println(a.member)
println(a.isEvaluated) // => false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With