ReactiveX / RxKotlin

RxJava bindings for Kotlin
Apache License 2.0
7.04k stars 455 forks source link

Remove T.toMaybe(), T.toSingle() #144

Closed thomasnield closed 6 years ago

thomasnield commented 7 years ago

We really should remove the following extensions:

https://github.com/ReactiveX/RxKotlin/blob/2.x/src/main/kotlin/io/reactivex/rxkotlin/single.kt#L9 https://github.com/ReactiveX/RxKotlin/blob/2.x/src/main/kotlin/io/reactivex/rxkotlin/maybe.kt#L9

I'm finding these quickly clash with other libraries and domains that have specialized types with good reason to have toSingle() and toMaybe(). For instance, a JavaFX popup Dialog could be extended to return a Maybe emitting the users response. A toMaybe() would be the natural name for this but it clashes with RxKotlin, and it is definitely clashing with RxKotlinFX.

Therefore, let's remove any extension functions targeting a general, single T type.

OrdonTeam commented 6 years ago

These extensions also collide in such case:

val toMaybe1: Maybe<Unit> = Single.just(Unit).toMaybe()
val toMaybe2: Maybe<Observable<Unit>> = Observable.just(Unit).toMaybe()
thomasnield commented 6 years ago

I'm following up on this stuff this weekend

onelaview commented 6 years ago

It seems these 2 following extensions collide with each other.

fun <T : Any> T?.toMaybe(): Maybe<T> = Maybe.create { s -> if (this != null) s.onSuccess(this); s.onComplete() }
fun <T : Any> (() -> T).toMaybe(): Maybe<T> = Maybe.fromCallable(this)

I'm unable to use the latter to apply .toMaybe() to a lambda so that it is the same as calling Maybe.fromCallable(lambda). Compiler always resolves it to the former extension.

val myInt: Int? = null
{ myInt }.toMaybe() // compiler resolves this to use the former extension function
                    // i.e. it produces Maybe<() -> Int?>
thomasnield commented 6 years ago

Yes, they both need to go away, all toMaybe() and toSingle() extensions.

alexbirkett commented 6 years ago

😭 I loved .toSingle()

thomasnield commented 6 years ago

@alexbirkett you can always implement it on your own domain. These extensions were highly problematic and clashed with other libraries that put toMaybe() and toSingle() on specific types.