Is there any way to write this: <https://glot.io/s...
# announcements
b
Is there any way to write this: https://glot.io/snippets/eya7pscezy without the
with
(not modifying the implementation of the
Weird
class) Not that I'd actually write this code
r
No, multiple receivers are not currently supported in Kotlin. There's been a fair amount of discussion about it, but I don't know if they ever will be.
b
What's the type of
withContext
in this example?
wouldn't the function type of that method have multiple receivers, or how does that work?
r
No,
withContext()
has a single receiver, but is defined within the scope of
Weird
, which creates another implicit receiver when you're not in the scope.
b
yep. so when you're not in the scope,
withContext
has two receivers, one implicit and one explicit?
r
Exactly
Kotlin does not allow you to specify multiple receivers, so you need to create an implicit receiver by creating a scope that has
this : Weird
, and
with
is an easy way to do that.
You could also do
x.apply { Weird("this-argument").withContext() }
or anything else that creates such a scope.
b
Is there any way to get a reference to that function? For normal member function, I can write
Weird::foo
to get a function, but this doesn't work for
withContext
r
No, that's something that bit me in the butt a while ago. Kotlin does not allow function references to functions with multiple receivers, and you can't even hack your way around it with
with
😞
b
<strike>ops, the
Weird::foo
only works for functions defined with
=
it seems</strike>
yeah it makes sense that it won't allow it because the type system probably cannot express the type of such a reference
r
Weird::foo
will work with any function (not just expression functions), so long as it doesn't violate some constraints like the multiple receivers.
b
I have another question that's somewhat related to this: is the
@lparams
necessary in this code: https://github.com/Kotlin/anko/blob/84e3838327011996aee24fb25f682fc7c1f7779e/anko/library/generated/sdk25/src/Layouts.kt#L95-L103 or was it just placed there for clarity?
r
No, but it helps distinguish that you mean the
this : T
and not the
this : _AppWidgetHostView
.
b
ok, that's what I thought as well, thank you!
👍 1