https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

jw

09/11/2017, 9:17 PM
can we get something like
ChannelIterable
that has
fun iterator(): ChannelIterator<T>
?
o

orangy

09/11/2017, 9:18 PM
And what’s
ChannelIterator
?
j

jw

09/11/2017, 9:20 PM
that's already in the coroutine library. It's basically
Iterator
but with
suspend
on the functions
o

orangy

09/11/2017, 9:22 PM
Ah, you mean just extract another interface from
ReceiveChannel
? Why?
j

jw

09/11/2017, 9:22 PM
So that I can implement it on custom types and you can do
for (value in myType)
instead of
for (value in myType.iterator())
o

orangy

09/11/2017, 9:24 PM
Is it polymorphic or something? You don’t need an
*Iterable
interface for this, just an
operator fun
with compatible signature
j

jw

09/11/2017, 9:27 PM
Oh cool I guess that makes sense. I assumed because of the suspension stuff that wouldn't work
Actually, I think one of the reasons I was looking for the interface was access to the context...
I don't know. I need to look later and try with what you said. I stepped away from my computer and am probably saying all the wrong things now.
o

orangy

09/11/2017, 9:30 PM
Just copy
iterator
signature from the interface and implement it, should just work without explicit call to
iterator()
in a
for
loop
j

jw

09/12/2017, 2:27 AM
Ok after more thought and less typing on a small screen what I want is impossible because I don't have a way to grab the coroutine context from the enclosing scope
Basically given a class I have no way of capturing the enclosing context when you call the method
I think I need multi-receiver extension functions to be able to capture the CoroutineScope off which I can pull the context and then the instance of my object. And that means you still need to call a method and can't just for-each it 😞
o

orangy

09/12/2017, 7:37 AM
@elizarov use case for that magic context property?
e

elizarov

09/12/2017, 8:01 AM
Yes. There are lots of them. See here: https://youtrack.jetbrains.com/issue/KT-17609
👍 2
4 Views