This expressions
The this expression refers to the current receiver (the object with which the function works). How you use this depends on the context:
In a member of a class,
thisrefers to the current object of that class.For example, in the following code,
this.namemeans thenameproperty of the currentLanguageobject:class Language(val name: String) { fun printName() { println(this.name) } } fun main() { val language = Language("Kotlin") language.printName() // Kotlin }In an extension function or a function literal with receiver,
thisrefers to the receiver.In the following example,
lastCharacter()is called on the string"Kotlin". Therefore,"Kotlin"is the receiver. Inside the extension function,thisrefers to"Kotlin". This means thatthis.lengthis the length of"Kotlin":fun String.lastCharacter(): Char { println(this.length) // 6 return this[this.length - 1] } fun main() { println("Kotlin".lastCharacter()) // n }
Qualified this
Your code can have several receivers available at the same time when receiver scopes are nested. Kotlin can use any available receiver to access its members implicitly, but receivers from inner scopes have higher priority. To explicitly refer to a particular receiver, use a qualified this. It is especially useful when multiple receivers have members with the same name, and you need to access a member of an outer receiver.
To use a qualified this, add a label qualifier:
The label tells the compiler which receiver to access. You can use the name of an enclosing class or extension function. For example, this@foo refers to the receiver of an enclosing extension function named foo.
Access an outer class from an inner class
In an inner class, unqualified this refers to the inner class instance. To access the outer class object, use qualified this:
Access a class from an extension function
If you declare an extension function inside a class, two receivers are available:
The extension receiver: the value on which you call the extension function.
The dispatch receiver: the current object of the class where you declare the extension function.
Use qualified this to specify which receiver you need:
Here:
thisrefers to the extension receiver (String), sothis.uppercase()converts the string to uppercase.this@Userrefers to the currentUserobject.this@User.prefixaccesses theprefixproperty of the currentUserobject.
Access from a lambda
Unlike a regular lambda, a lambda with receiverintroduces a receiver into its scope. As a result, this inside the lambda refers to the lambda's receiver, not to one from an enclosing scope. If a lambda with receiver is nested inside another receiver scope, add a label to the lambda and use a qualified this to refer explicitly to the lambda's receiver or to a receiver from an enclosing scope:
Here:
stringLabel@labels the lambda.this@stringLabelrefers to the string receiver of the lambda.this@Userrefers to the currentUserobject.
The label doesn't call anything or change how the lambda works. It only helps you refer to the lambda's receiver.
Access an anonymous object or its outer class
An anonymous object has its own receiver scope. Inside the object body, unqualified this refers to the anonymous object itself. However, since anonymous objects don't have class names, you can't use them as this qualifiers. Therefore, you can refer to the anonymous object only with unqualified this:
If you declare an anonymous object inside another class, you can use qualified this to access the enclosing object:
Implicit this
When you call a member function on this, you can omit the this. qualifier. However, if another callable with the same name is available in a closer lexical scope, Kotlin resolves the unqualified call to that callable instead of the member function. To explicitly call the member function, use the this. qualifier: