Rxjava: 2.0.9 possible bug with Maybe.switchIfEmpty

Created on 29 May 2017  路  2Comments  路  Source: ReactiveX/RxJava

So, not sure if it's a bug or am I not using the operator right.

Maybe.empty<Int>()
        .switchIfEmpty { Maybe.just(2) }
        .subscribe {
            println("got $it")
        }

Block above completes without any results as if Maybe is empty. While block below completes successfully with got 2 output.

Maybe.empty<Int>()
        .switchIfEmpty(Maybe.just(2))
        .subscribe {
            println("got $it")
        }

From documentation I expect, that both cases should work the same.

2.x Kotlin Question

Most helpful comment

The first case creates a lambda that does nothing, courtesy of Kotlin I guess. If written out in Java syntax:

.switchIfEmpty(mo -> { Maybe.just(2); })

where you are supposed to signal on mo according to the Maybe protocol but you just create and throw away a Maybe instance.

All 2 comments

The first case creates a lambda that does nothing, courtesy of Kotlin I guess. If written out in Java syntax:

.switchIfEmpty(mo -> { Maybe.just(2); })

where you are supposed to signal on mo according to the Maybe protocol but you just create and throw away a Maybe instance.

You're right, but, lambda does something.
.switchIfEmpty { ... } creates MaybeSource where subscribe is executed and Maybe.just(2) is created, but nothing happens after that.
so in this case:
.switchIfEmpty { it.onSuccess(2) } and .switchIfEmpty(Maybe.just(2)) are the same.

Thanks for pointing out my mistake.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hoc081098 picture hoc081098  路  3Comments

dsvoronin picture dsvoronin  路  4Comments

dzharikhin picture dzharikhin  路  4Comments

perlow picture perlow  路  3Comments

midnight-wonderer picture midnight-wonderer  路  3Comments