Screenshot 2019-03-29 at 00.22.18.png
# announcements
d
Screenshot 2019-03-29 at 00.22.18.png
b
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
Copy code
val third = Third()
third.print(third)
d
I posted an example code, may you could take a lock
s
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
@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
Not really... You could make it a regular method, not an extension function, and add a parameter to it:
fun print(receiver: First) = ...
d
That won't make any sense in my use case 😋