Frankie Murillo
05/19/2023, 12:26 AMDeferred<String>
, Deferred<Boolean>
and am expecting it to return a List, but the result type seems to be of List<{Comparable<{Boolean & String}> & java.io.Serializable}>
is there something I am missing here?Jacob
05/19/2023, 12:57 AMJoffrey
05/19/2023, 1:10 AMval s = stringDeferred.await()
val b = booleanDeferred.await()
Pair<String, Boolean>
) instead of a list. Lists are meant for homogeneous types of items.Frankie Murillo
05/19/2023, 1:29 AMJacob
05/19/2023, 1:35 AMFrankie Murillo
05/19/2023, 1:47 AMpublic suspend fun <T> awaitAll(
vararg deferreds: Deferred<T>
): List<T>
Jacob
05/19/2023, 1:49 AMephemient
05/19/2023, 5:29 AMT
must be a single type. Kotlin tries to infer the narrowest type (least upper bound) it can: https://kotlinlang.org/spec/type-inference.html#local-type-inference https://kotlinlang.org/spec/kotlin-type-constraints.html#finding-optimal-constraint-system-solution
which leads to this intersection typeString
and Boolean
are both Serializable
(on JVM)
• String
is Comparable<String>
and Boolean
is Comparable<Boolean>
, so they are both Comparable<Boolean & String>
◦ e.g. comparable to something which is simultaneously both a boolean and a string at the same time. no such value exists, but that doesn't matter for typechecking
• those are all of the common supertypes of String
and Boolean
(other than `Any`/`Object`)
• so the least upper bound is T = Comparable<Boolean & String> & Serializable