Recently, I’ve had a huge annoyance: I wanted to use a lazy value and smart cast it. A lazy value computes its result once and then always returns the same value, so I thought it could surely be smart-casted. Turns out, it can’t. Why?
Is there simply no possibility for the compiler to express such a thing? I’d rather not use
let blocks or local variables everytime I access that lazy, since I might as well use a var if I have to check it all the time anyways.
The problem why I can’t do it differently is that I am writing an android application and I have a variable that has to be initialized in onCreate. As such it can’t be a simple
val, since it is not initialized in the constructor, yet it does not change its value ever after creation. I opted for lazy since that also implies it to be a
val, but Kotlin refuses to treat it as “real”