y
05/11/2025, 7:31 AMclass Foo {
class Bar {
class Baz : Bar()
}
fun Bar.Baz.funName() { /* ... */ }
}
this
or this@Baz
can be used to refer to the Baz
(and this@Foo
is used to refer to the parent)
however this@funName
can also be used for Baz
, what's the rationale here? that's kind of strange.Joffrey
05/11/2025, 9:51 AMFoo
, because funName
is a method of the class Foo
, and an instance of Baz
because funName
declares Baz
as a receiver.
Note that the Bar
nesting is irrelevant for this question, by the way, and so is declaring `Bar`/`Baz` inside Foo
. It's not about the nesting of the classes, but just about referring to the correct receiver.
The example works just as well when expressed this way, which might makes things clearer:
class Foo {
fun Baz.funName() { ... }
}
class Baz
Now, this is the same: still 2 receivers, instances of Foo
and Baz
. I don't know exactly why both syntaxes are allowed for Baz
, but I imagine you can use the function name becauss this receiver is declared on the function itself (unlike Foo
which is present for all methods of the class)y
05/11/2025, 10:22 AM