1

I need to convert a generic Flux (from reactor.core) to kotlinx.coroutines.flow.Flow

I'm trying this code

    import kotlinx.coroutines.reactive.asFlow

    fun <T> genericFlux2FLow (flux: Flux<T>) {
     flux.asFlow()
    }

and the compiler says "Unresolved reference" for asFlow.

This code works though

  fun flux2Flow(flux: Flux<Int>) {
     flux.asFlow()
}
Michele Da Ros
  • 856
  • 7
  • 21
  • 1
    Maybe use `return`. :) Ofc, you don't get flow object if you never return an object. – Numichi May 12 '23 at 11:25
  • 1
    I think it it can't handle your possibly-nullable `T`, but what is the point of creating a function that simply calls another function that does exactly the same thing? – Tenfour04 May 12 '23 at 14:56
  • Did you remember to include the `org.jetbrains.kotlinx:kotlinx-coroutines-reactive` library as a dependency of your project? – dnault May 12 '23 at 17:08

1 Answers1

2

You should change your generic to T : Any

fun <T: Any> genericFlux2FLow(flux: Flux<T>): Flow<T> = flux.asFlow()
Yevhenii Semenov
  • 1,587
  • 8
  • 18
  • 1
    Wow it works, thanks! Now the question is... Why? :) – Michele Da Ros May 12 '23 at 20:55
  • `a(): Type {...}` so `fun func() = a()` is equals `fun func(): Type { return a() }` – Numichi May 13 '23 at 17:11
  • 2
    @MicheleDaRos this is because extension function `asFlow()` can only be applied to Publisher type (it means only to not-nullable types). But in your initial example, your T type can be nullable. That's why the compiler didn't recognize it. – Yevhenii Semenov May 13 '23 at 23:03