Is it possible in some way to get access to the ou...
# announcements
j
Is it possible in some way to get access to the outer class properties when creating an extension function on an inner class?
Copy code
class Outer(var value: String = "") {
    inner class Inner {
        var id = 0
        val str: String get() = value + id
    }
}
fun main(args: Array<String>) {
    fun Outer.Inner.update() = also { value = "Hello World" } // I don't have access to `value` here :(
    println(Outer().Inner().update().str)
}
a
think you might have to expose the outer
Copy code
class Outer(var value: String = "") {
    inner class Inner {
        var id = 0
        val str: String get() = value + id
        val outer = this@Outer
    }
}

fun main(args: Array<String>) {
    fun Outer.Inner.update() = also { outer.value = "Hello World" }
    println(Outer().Inner().update().str)
}
j
I did that as a workaround, but when trying to achieve something like this on a 3rd party library, it would not be possible.
d
On the JVM the first field is the outer class instance