I’ve got an object that has some expensive-to-compute values which rely on each other. So I considered making them by lazy
. And I can make sure that only one thread is calculating it with the default LazyThreadSafetyMode.SYNCHRONIZED
But in the chain of myClass.c depends on myClass.b depends on myClass.a, I don’t think I can say “b changed, which invalidated c”. I think I need a way to say “clear out the lazy value and recompute next time it is requested”
I hacked something together. It isn’t pretty, or synchronized, and all users of this solution need to !! to avoid nulls that will never happen. Is there a better way to do this? A custom byLazyButResettable perhaps?
private var b: MyThing? = null
get() = field ?: a!!.doStuff().also { field = it }
set(value) {
c = null
field = value
}