https://kotlinlang.org logo
Title
k

Karolo

10/11/2017, 11:31 PM
Hi, I will jump into discussion with my question, so if iterrupting I am sorry 🙂 I have in object Foo var with custom setter:
var foo: ((Long) -> Unit)? = null
        set(value) {
            update()
        }
And the problem is when setting it from another part of code like that:
Foo.foo = {it-> doSomeJobWithIt(it) }
it still after such verified call stays null. I am sure that setter is called but somehow my foo inside Foo is still null. Is there something I am missing with variables in object's ? Thanks in advance!
k

karelpeeters

10/11/2017, 11:37 PM
Surround code with ```.
To actually answer you question, your setter needs to actually set the field as well, the backing field is available under the name
field
. Add this as the first line of your setter:
field = value
.
k

Karolo

10/12/2017, 8:36 AM
@karelpeeters ehh I was missing that field name, was trying with var name but then ofcourse I got stackoverflow from recursive calls.