https://kotlinlang.org logo
#announcements
Title
# announcements
t

tseisel

10/22/2019, 6:45 PM
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

Stephan Schroeder

10/23/2019, 9:18 AM
sourceFlow.map { Request.Success(it) }
works fine for me
t

tseisel

10/23/2019, 12:28 PM
@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

Stephan Schroeder

10/23/2019, 12:50 PM
I just checked if IntelliJ flagged an error.
an because I don’t have the Flow dependency, I used a List<Foo> instead
2 Views