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

Matt Thiffault

10/21/2019, 7:55 PM
Is there a way to use select (or some other similar mechanism) to select for messages on an arbitrary set of Channels not known at compile time (provided the channels have the same message type)?
o

octylFractal

10/21/2019, 8:11 PM
what's wrong with using
select
?
you can just do
for (c in list) c.onX {}
m

Matt Thiffault

10/21/2019, 8:12 PM
Ah, I hadn't found any docs which did something like that, wasn't sure if calling onReceive in a loop in a select block would do what I want
If it will, awesome
It looked like you had to explicitly do select<Type> { foo.onReceive... bar.onReceive... baz.onReceive... ....
o

octylFractal

10/21/2019, 8:13 PM
there's no magic language feature, it's just library code , so I presume it will work because it's just like listing individually
b

bdawg.io

10/21/2019, 9:45 PM
The following works as well
Copy code
val receivingA: SelectClause1<Foo> = channel.onReceive
select { // this: SelectBuilder<Foo>
    receivingA { it }
}
The key thing is that you
invoke
on the SelectClause returned by the
onReceive
property from inside of the
SelectBuilder
context
6 Views