sealed class LoadRequest<out T> {
object Pending : LoadRequest<Nothing>()
class Success<T>(val data: T) : LoadRequest<T>()
class Failure(val error: Exception) : LoadRequest<Nothing>()
}
val someFlow: Flow<Foo> = ...
val requestFlow: Flow<LoadRequest<Foo>> = someFlow
.map { LoadRequest.Success(it) as LoadRequest<Foo> }
.onStart { emit(LoadRequest.Pending) }
.catch { emit(LoadRequest.Failure(it) }
The
as LoadRequest<Foo>
part is marked as
No cast needed
, but if I remove it then I get
Type mismatch
on
LoadRequest.Pending
and
LoadRequest.Failure
(it expects a
LoadRequest.Success<Foo>
).
Is it a compiler bug or is there a workaround ? I'd expect the compiler to figure out the proper type by analyzing the whole operator chain.
d
dmitriy.novozhilov
03/16/2021, 4:19 PM
Inference of call chains is unsupported (it's very complex feature): https://youtrack.jetbrains.com/issue/KT-17115
As a workaround for removing warnings you can specify type parameter of `map`: