tseisel
03/16/2021, 3:58 PMFlow
operator chain:
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.dmitriy.novozhilov
03/16/2021, 4:19 PMmap<LoadRequest<Foo>, LoadRequest<Foo>> { LoadRequest.Success(it) }
or just suppress warning with @Suppress("USELESS_CAST")