Kotlin type mismatch compile error: Require Success<T>, Found MyError

Viewed 3777

I am encountering an issue that the following code can't be compiled in kotlin.

// StateModel.kt
sealed class StateModel
class Loading : StateModel()
data class Success<T: Any>(val data: T) : StateModel()
data class MyError(val message: String) : StateModel()

// StateModelTransformer.kt
class StateModelTransformer<T: Any> : FlowableTransformer<T, StateModel> {

  override fun apply(upstream: Flowable<T>): Publisher<StateModel> {
    return upstream
      .map { data -> Success(data) }
      .onErrorReturn { error ->
        MyError(error.message)  // compile error, Type mismatch, Require Success<T>, Found MyError
      }
      .startWith(Loading()) // compile error, none of the following function can be called with the arguments supplied
  }

}

I have no idea why the onErrorReturn says requiring a Success<T> type but a StateModel type.

Thanks

1 Answers
Related