Why do I need to cast the result of the `map` oper...
# announcements
t
Why do I need to cast the result of the
map
operation in the following sample :
Copy code
sealed class Request<T> {
    object Pending : Request<Nothing>()
    class Success<T>(val result: T) : Request<T>()
    class Failure(val error: Throwable) : Request<Nothing>()
}

val sourceFlow: Flow<Foo> = getFooFlow()
sourceFlow.map { Request.Success(it) as Request<Foo> }
    .onStart { emit(Request.Pending) }
    .catch { emit(Request.Failure(it) }
    .onEach { ... }
    .launchIn(scope)
Will this kind of issue be fixed by the new Kotlin inference ? Or is there a workaround (other than creating a typed temporary variable) ?
s
sourceFlow.map { Request.Success(it) }
works fine for me
t
@Stephan Schroeder have you tried with
onStart
, just like in the sample code ? This should trigger a compile error, as
Request.Pending
is not a
Request.Success
.
s
I just checked if IntelliJ flagged an error.
an because I don’t have the Flow dependency, I used a List<Foo> instead