I'm trying to implement ```suspend fun <R> r...
# coroutines
r
I'm trying to implement
Copy code
suspend fun <R> race(vararg races: suspend () -> R): R {
  return channelFlow {
    for (race in races) {
      launch { send(race()) }
     }
   }.first()
 }
for my little interpreter:
Copy code
@Serializable
    sealed interface Logic: Command {
        @Serializable
        @SerialName("Race")
        // race any number of commands, will return as soon as the first one happens.
        data class Race(val commands: List<Command>): Logic
    }
But the
run
doesn't really make the types align:
Copy code
is Command.Logic.Race -> {
            race(commands.map { command -> command.run() }.toTypedArray())
        }
d
That's because
race
accepts functions, but you're passing it an array of values. Please try
{ command -> { command.run() } }
instead of
{ command -> command.run() }
.
r
Not sure how to align the syntax properly.
d
{ command -> suspend { command.run() } }
?
r
That worked, had to add
*
for the varargs
j
Wouldn't you want to use someting like
select
for this?
d