Skip to content

[upd] this expressions#5657

Open
Alex Epanchintseva (aepanchi) wants to merge 4 commits into
masterfrom
aepanchi/this-expressions
Open

[upd] this expressions#5657
Alex Epanchintseva (aepanchi) wants to merge 4 commits into
masterfrom
aepanchi/this-expressions

Conversation

@aepanchi

Copy link
Copy Markdown
Contributor

No description provided.

@aepanchi Alex Epanchintseva (aepanchi) requested a review from a team as a code owner July 6, 2026 12:36
Comment thread docs/topics/this-expressions.md Outdated
* In an [extension function](extensions.md) or a [function literal with receiver](lambdas.md#function-literals-with-receiver)
`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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually very important: this doesn't only refer to the current receiver. It actually can refer to any of the nested receivers. For example, if I have

class Foo {
  fun foo() { }
}

class Bar {
  fun bar() { }

  fun Foo.moo() {
    this.bar()  // 'this' refers to the enclosing 'Bar' instance
    this.foo()  // 'this' refers to the extension receiver
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to the Qualified this section. Do you think it also makes sense to include the example like the one you've provided?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this is incorrect. You write "If this has no qualifier, it refers to the receiver of the innermost enclosing scope", but my example above shows that this may refer to different enclosing scopes, even if it appears unqualified.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it now, thank you! What do you think about smth like this:

Kotlin code can have several receivers available at the same time. This happens when receiver scopes are nested. For example, an extension function declared inside a class can access members of both the extension receiver and the enclosing class object.
Kotlin can use these receivers implicitly when you access their members. Unqualified this refers to the receiver with the highest priority in the current scope. To refer to another receiver explicitly, use a qualified this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with your comment is in the "Unqualified this refers to the receiver with the highest priority in the current scope." part, unqualified this may refer to any receiver in scope, the closest one is just given more priority. So if, for example, several receivers have the same type, using the qualified this is the only possibility to access the outer one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah in that case my wording is too restrictive ig, so is the following would be correct:

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. This is especially useful when multiple receivers have members with the same name and you need to access a member of an outer receiver.

Comment thread docs/topics/this-expressions.md Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants