Pretty sure I broke switchIfEmpty :confused: ``` f...
# rx
b
Pretty sure I broke switchIfEmpty 😕
Copy code
fun doThings(): Maybe<Boolean> {
    Log.d("TEST", "HIT")
    return Maybe.just(true) 
}

return Maybe.just("")
    .map { false }
    .switchIfEmpty(doThings())
My log message still shows even though my Maybe is not empty
k
what you do here is
Copy code
.switchIfEmpty({ 
    Log.d("TEST", "HIT")
    return Maybe.just(true) 
  }()
)
note that to be able to return
Maybe.just(true)
you have to execute the
doThings()
function which triggers the log all the time.
☝️ 1
switchIfEmpty
expects an observable as argument
doThings()
eagerly called when the return statements called
b
Is there a way to defer it?
k
fix for the expected behaviour:
Copy code
fun doThings(): Maybe<Boolean> =
Maybe.just(true)
    .doOnNext{ Log.d("TEST", "HIT") }
b
Okay, that’s an interesting approach
k
doThings()
will execute to generate a maybe. but the maybe will only be executed if
switchIfEmpty
triggers it which triggers the log
b
I get what you’re saying. I guess I expected it to not even get to the switchIfEmpty until it was actually empty
Thank you for your help!
👍 1
k
add(1, 1.inc())
<-- here we expect that
inc()
will be executed before
add
. same is true for
switchIfEmpty
func(...) \\ "..." is always eager unless it is wraps the code in lazy evaluation
b
yup, that makes sense to me