RxSwift icon indicating copy to clipboard operation
RxSwift copied to clipboard

Fix Infallible's flatMap family of operators that can return an Infallible that is not, in fact, infallible

Open nikolaykasyanov opened this issue 1 year ago • 1 comments

I believe it's a big hole in the Infallible contract because this will a) compile b) return an Infallible that will fail silently, without calling any subscription closures (well maybe it will call onDispose[d], though, but that's beside the point):

_= someInfallible.flatMap { Observable.error(anError) }
    subscribe(onCompleted: { print("it's not gonna call me") }) 

There's two ways to fix this:

  1. Have overloads for both variants, i.e.
    1. a flatMap that expects its closure to only return Infallible streams, and that itself returns Infallible.
    2. a flatMap that expects its closure to return any ObservableConvertibleType and itself returns Observable.
  2. Only have the first of the above.

While 1. is better for backwards compatibility, it complicates overload resolution (Infallible is ObservableConvertibleType), although it seems to be working in the test code I've written.

As soon as the proposed change is agreed on, I'll add tests for the first and latest variants.

nikolaykasyanov avatar Jul 18 '23 09:07 nikolaykasyanov

I think the fix could make sense indeed, but unfortunately it's a breaking API change so we can't easily merge it right now. It will have to wait for a later major version, but I'm keeping it open to not forget. Thanks!

freak4pc avatar Apr 20 '24 20:04 freak4pc