Skip to content

Instantly share code, notes, and snippets.

@efemoney
Last active July 12, 2025 21:21
Show Gist options
  • Select an option

  • Save efemoney/675eda45655279d0dc99d3522a408209 to your computer and use it in GitHub Desktop.

Select an option

Save efemoney/675eda45655279d0dc99d3522a408209 to your computer and use it in GitHub Desktop.
Coroutines `awaitAll` with type information
import kotlinx.coroutines.Deferred
suspend inline fun <T1, T2> awaitAll(t1: Deferred<T1>, t2: Deferred<T2>) =
kotlinx.coroutines.awaitAll(t1, t2).let { Pair<T1, T2>(it) }
suspend inline fun <T1, T2, T3> awaitAll(t1: Deferred<T1>, t2: Deferred<T2>, t3: Deferred<T3>) =
kotlinx.coroutines.awaitAll(t1, t2, t3).let { Triple<T1, T2, T3>(it) }
suspend inline fun <T1, T2, T3, T4> awaitAll(t1: Deferred<T1>, t2: Deferred<T2>, t3: Deferred<T3>, t4: Deferred<T4>) =
kotlinx.coroutines.awaitAll(t1, t2, t3, t4).let { Quadruple<T1, T2, T3, T4>(it) }
suspend inline fun <T1, T2, T3, T4, T5> awaitAll(
t1: Deferred<T1>,
t2: Deferred<T2>,
t3: Deferred<T3>,
t4: Deferred<T4>,
t5: Deferred<T5>,
) = kotlinx.coroutines.awaitAll(t1, t2, t3, t4, t5).let { Quintuple<T1, T2, T3, T4, T5>(it) }
@JvmInline
value class Pair<T1, T2>(val list: List<Any?>) {
inline operator fun component1() = list[0] as T1
inline operator fun component2() = list[1] as T2
}
@JvmInline
value class Triple<T1, T2, T3>(val list: List<Any?>) {
inline operator fun component1() = list[0] as T1
inline operator fun component2() = list[1] as T2
inline operator fun component3() = list[2] as T3
}
@JvmInline
value class Quadruple<T1, T2, T3, T4>(val list: List<Any?>) {
inline operator fun component1() = list[0] as T1
inline operator fun component2() = list[1] as T2
inline operator fun component3() = list[2] as T3
inline operator fun component4() = list[3] as T4
}
@JvmInline
value class Quintuple<T1, T2, T3, T4, T5>(val list: List<Any?>) {
inline operator fun component1() = list[0] as T1
inline operator fun component2() = list[1] as T2
inline operator fun component3() = list[2] as T3
inline operator fun component4() = list[3] as T4
inline operator fun component5() = list[4] as T5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment