Hello :wave: Just wondering whether this differenc...
# coroutines
d
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
Can you run a null check before calling
.asPublisher
?
d
issue is with generics and type erasure