Files
kotlin-fork/docs/confluence.jetbrains.com/Kotlin/40700138_This+expressions.confluence
T
2012-05-30 14:41:40 +04:00

42 lines
1.6 KiB
Plaintext

To denote the current _receiver_, we use *this* expressions:
* In a member of a [class|Classes and Inheritance], *this* refers to the current object of that class
** *this{*}{{<Supertype>}} is used to qualified calls to supertype's implementations of members, see [Classes and Inheritance#Overriding rules]
* In an [extension function|Extension functions] or an [extension function literal|Function literals#Extensions], *this* denotes the _receiver_ parameter that is passed on the left-hand side of a dot.
If *this* has no qualifiers, it refers to the _innermost enclosing scope_. To refer to *this* in other scopes, _label qualifiers_ are used:
{anchor:qualified}
h3. Qualified {{this}}
To access *this* from an outer scope (a [class|Classes and Inheritance], or [extension function|Extension functions], or labeled [extension function literal|Function literals#Extensions]) one writes *this{*}{{@label}} where {{@label}} is a [label|Returns and jumps] on the scope {{this}} is meant to be from:
{jet}
class A { // implicit label @A
class B { // implicit label @B
fun Int.foo() { // implicit label @foo
val a = this@A // A's this
val b = this@B // B's this
val c = this // foo()'s receiver, an Int
val c1 = this@foo // foo()'s receiver, an Int
val funLit = {String.() -> // implicit label @
val d = this // funLit's receiver
val d1 = this@ // funLit's receiver
}
val funLit2 = { (s:String) ->
val d1 = this // foo()'s receiver, since enclosing function literal doesn't have any receiver
}
}
}
}
{jet}
h3. What's next
* [Tuples]
* [Type casts]