Getting a this pointer to the enclosing lambda

Hi there,

consider the following code where you connect and disconnect listeners (which are just functions):

fun connect(f: () -> Unit) {}
fun disconnect(f: () -> Unit) {}

fun test() {
    connect(object : () -> Unit {
        override fun invoke() {
            // ...
            disconnect(this) // works
        }
    })
    connect({
         // ...
        disconnect(this) // error: `this` is not defined in this context
    })
}

Only the version that’s using the anonymous class works.
Is there a more elegant way to handle this?

No. This was already discussed here.