Given the following sealed class definition and `F...
# getting-started
t
Given the following sealed class definition and
Flow
operator chain:
Copy code
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
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`:
map<LoadRequest<Foo>, LoadRequest<Foo>> {  LoadRequest.Success(it) }
or just suppress warning with
@Suppress("USELESS_CAST")
💯 1