I am migrating codebase from Rx1 to Rx2 and now th...
# announcements
a
I am migrating codebase from Rx1 to Rx2 and now that
null
is not supported by RxJava and RxKotlin, what is the strategy you use to have kind of null values? We have some places in codebase that were doing something like
Copy code
map {
  service.get() //can return null
}.filterNotNull()
It's too much effort to rewrite every service to not ever return null when before null was considered a valid empty value.
Using Java
Optional
in Kotlin kind of makes me shiver.
j
We use Koptional from Juno. I don't see a problem with using an optional abstraction in Kotlin: https://developer.squareup.com/blog/an-optionals-place-in-kotlin/
👍 1
p
You can declare your own
Optional
Copy code
data class Optional<out T>(val value: T?)

val <T> T?.optional get() = Optional(this)

fun <T> Observable<T>.startWithNull(): Observable<Optional<T>> = map { it.optional }.startWith(null.optional)
fun <T> Observable<Optional<T>>.filterNotNull(): Observable<T> = filter { it.isNotNull }.map { it.value!! }
a
Feels like I should rather go to Coroutines than RxJava 2, but the codebase is far from being ready for that
j
Yep, Flow is a good choice.