If I have something like this: ```class Foo { ...
# getting-started
j
If I have something like this:
Copy code
class Foo {
    var number: Int = 0
}
Is it possible to pass a reference to the
number
property to another class so that the other class can reassign it?
m
I believe you can pass a
KMutableProperty1
to a function and get an instance of from
::number
. You can also access the setter from it pass it somewhere as a
Function
.
s
or you pass a lambda around:
Copy code
val foo = Foo()
val setter: (Int)->Unit = { newValue -> foo.number = newValue }
class Bar( val setNumber: (Int)->Unit )
val bar = Bar(setter)
r
Here's an example of what @Michael Krussel said. You can use
::number::set
to get a `(Int) -> Unit`:
Copy code
fun main() {
    val foo = Foo()
    val bar = Bar()
    bar.run(foo::number::set)
    println(foo.number)
}

class Foo {
    var number: Int = 0
}

class Bar {
    fun run(consumer: (Int) -> Unit) {
        repeat(5) {
            consumer(it)
        }
    }
}
2