https://kotlinlang.org logo
Title
d

Davide Giuseppe Farella

03/28/2019, 11:22 PM
b

bdawg.io

03/28/2019, 11:23 PM
you are not in context of your
Third
instance, so no
that’s a disadvantage of having extension function defined within a type
This should be available, but I haven’t had any reason do anything like this since Kotlin 1.3 came out
val third = Third()
third.print(third)
d

Davide Giuseppe Farella

03/28/2019, 11:26 PM
I posted an example code, may you could take a lock
s

streetsofboston

03/29/2019, 12:26 AM
The
fun First.print()
is an extension function defined in a class/interface and therefore it has two receivers. One must be an implicit instance-receiver (of type
Second
), the second one must be the explicit receiver that is extended (of type
First
). The only way to call a function with multiple receivers is to be ‘in the context of the instance-receiver’ and then provide the second one. Where you call it (
Third().print()
), you only provide one receiver, the explicit extended receiver, and you are not in the context of any (implicit) instance-receiver. Inside the lambda of the
with
function, you have both receivers (and they are the same instance of course). I do agree, though, that either both statements should compile or none of them. Because in the
with
lambda, you don’t provide an explicit receiver either (the implicit
this
is used). Interesting… 🙂
d

Davide Giuseppe Farella

03/29/2019, 7:55 AM
@streetsofboston have you some solution in mind for avoid to duplicate many functions? Basically I have to create shadow functions like
fun Third.print() = print()
🤯
s

streetsofboston

03/29/2019, 11:49 AM
Not really... You could make it a regular method, not an extension function, and add a parameter to it:
fun print(receiver: First) = ...
d

Davide Giuseppe Farella

03/29/2019, 11:54 AM
That won't make any sense in my use case 😋