3

I tried to create an alias function to Flowable.flatmap() as follow, but compile error.

fun <T, R> Flowable<T>.then(mapper: Function<T, Publisher<R>>): Flowable<R> {
  return flatMap(mapper)
}

The error is : One type argument expected for interface Function<out R> defined in kotlin

Have any idea? Thanks!

jaychang0917
  • 1,880
  • 1
  • 16
  • 21

1 Answers1

1

The flatMap takes a java.util.function.Function, the actually error is you didn't import the java.util.function.Function in your Kotlin file, but I don't suggest you use the functions because you can't take advantage of the SAM Conversions to use the lambda directly from the Kotlin code which defined with functional interface as parameter type.

You should replace Function with Function1, since the Function interface is a Kotlin marker interface only. for example:

//                                  v--- use the `Function1<T,R>` here
fun <T, R> Flowable<T>.then(mapper: Function1<T, Publisher<R>>): Flowable<R> {
    return flatMap(mapper)
}

OR use the Kotlin function type as below, for example:

//                                      v--- use the Kotlin function type here  
fun <T, R> Flowable<T>.then(mapper: (T) -> Publisher<R>): Flowable<R> {
    return flatMap(mapper)
}
holi-java
  • 29,655
  • 7
  • 72
  • 83