blakelee
08/19/2019, 4:19 PMfun 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 emptykioba
08/19/2019, 4:21 PM.switchIfEmpty({
Log.d("TEST", "HIT")
return Maybe.just(true)
}()
)kioba
08/19/2019, 4:22 PMMaybe.just(true) you have to execute the doThings() function which triggers the log all the time.kioba
08/19/2019, 4:23 PMswitchIfEmpty expects an observable as argument doThings() eagerly called when the return statements calledblakelee
08/19/2019, 4:24 PMkioba
08/19/2019, 4:24 PMfun doThings(): Maybe<Boolean> =
Maybe.just(true)
.doOnNext{ Log.d("TEST", "HIT") }blakelee
08/19/2019, 4:25 PMkioba
08/19/2019, 4:26 PMdoThings() will execute to generate a maybe. but the maybe will only be executed if switchIfEmpty triggers it which triggers the logblakelee
08/19/2019, 4:26 PMblakelee
08/19/2019, 4:27 PMkioba
08/19/2019, 4:29 PMadd(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 evaluationblakelee
08/19/2019, 4:32 PM