Is there a workaround for this issue? ``` class My...
# announcements
k
Is there a workaround for this issue?
Copy code
class MyClass {
    var a: Int
    
    init {
        computeA()
    }

    private fun computeA() {
        a = 5
    }
}
The compiler complains that I have to initialize
a
, apparently it can't tell
computeA()
does initialize it.
a
@karelpeeters use
lateinit var
modifier
👎 1
k
I can't believe I didn't think of that! I've been abusing
lateinit
a bit lately so I forgot it has legitimate use cases too simple smile.
Thanks a lot @asimaruk
👌 1
d
To be honest, this doesn't sound like a legitimate use-case for lateinit. Why not
var a = computeA()
?
👍 1
k
Because I computeA() will be called from a lot of other setters. Basically is the result of an expensive calculation on a couple of other variables, and I want to reduce boilerplate.
a
Use lazy
k
Does lazy have an invalidate method or something? I want the field to change as others change as well.
d
Ok, Then lateinit seems fine 🙂
a
Check your custom setters. They won't assign a new value to property. Better use
var position by Delegates.observable(position) { prop, old, new -> updateView() }
👍 1