I want to create an extension function that is usi...
# getting-started
v
I want to create an extension function that is using the
apply{}
function inside. Like:
Copy code
fun Foo.myFun() {
  bar.apply {
    this.doA()
    this.doB()
    this.doC()
  }
}
this
here refers to the type of
bar
, not to the type of
Foo
How can I use the instance of
Foo
inside the
bar.apply{}
?
j
You can use the labeled syntax
this@myFun
to refer to the receiver of the function. But this is only necessary if there is an ambiguity. You can also not use
this
at all and simply call the methods/properties of
Foo
directly
v
Thanks! Didn’t that I can call functions of
Foo
w/o specifying
this
explicitly
j
Yep, in Kotlin in general (like in Java) using
this
is optional, unlike in JS. It's only necessary when there is ambiguity