Using 'this' inside a lambda with receiver inside member function


#1

Hi! I there any way to reference a member fun’s this from a lambda with another receiver? Thanks!

class MyClass {
    fun member() {
        funTakingLambdaWithReceiver {
            // do something with member fun's this here
        }
    }
}

#2

http://kotlinlang.org/docs/reference/this-expressions.html#qualified


#3

@cretz

Thanks for referring the doc. I did take a look at the doc before posting the question, but couldn’t find the solution I was looking for. What’s missing is the name of the implicit label for MyClass which can be referenced from funTakingLambdaWithReceiver. Later I figured out that the label is the name of the class preceded by @.

class MyClass {
    fun member() {
        funTakingLambdaWithReceiver {
            this // refers to the receiver of funTakingLambdaWithReceiver
            this@MyClass // refers to the invoking MyClass instance
        }
    }
}