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

Dariusz Kuc

12/16/2020, 5:33 PM
Hello 👋 Just wondering whether this difference in behavior is expected, i.e. given flow <-> publisher interop
Copy code
public fun <T : kotlin.Any> org.reactivestreams.Publisher<T>.asFlow(): kotlinx.coroutines.flow.Flow<T>
public fun <T : kotlin.Any> kotlinx.coroutines.flow.Flow<T>.asPublisher(): org.reactivestreams.Publisher<T>
This is valid
Copy code
// inferred -> flow: Flow<Any?>?
val flow = when (val publisherOrFlow: Any? = fetchValue()) {
    is Publisher<*> -> publisherOrFlow.asFlow()
    is Flow<*> -> publisherOrFlow
    else -> null
}
Yet this is invalid
Copy code
// inferred -> publisher: Publisher<*>?
val publisher = when (val publisherOrFlow: Any? = fetchValue()) {
    is Publisher<*> -> publisherOrFlow
    is Flow<*> -> publisherOrFlow.asPublisher() // <-- issue here -> inferred type Any? is not a subtype of Any
    else -> null
}
guess its because
Publisher
is Java interface?
s

Shane Myrick

12/16/2020, 7:03 PM
Can you run a null check before calling
.asPublisher
?
d

Dariusz Kuc

12/16/2020, 7:33 PM
issue is with generics and type erasure
2 Views