Skip to end of metadata
Go to start of metadata

To denote the current receiver, we use this expressions:

If this has no qualifiers, it refers to the innermost enclosing scope. To refer to this in other scopes, label qualifiers are used:

Qualified this

To access this from an outer scope (a class, or extension function, or labeled extension function literal) one writes this@label where @label is a label on the scope this is meant to be from:

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 
      } 
    } 
  } 
}

What's next

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.