What would be the best way to convert Java Optional
to Arrows Option
? I was expected to have something out-of-box, but it's not there. Something like:
fun <T> Optional<T>.toOption(): Option<T> = if (this.isPresent) Some(this.get()) else none()
CodePudding user response:
There is no such function at the moment, but such a contribution would be welcomed!
Arrow however does not recommend using Option
unless absolutely necessary. The only use-case being nested nulls, which is the limitation for ReactiveX implementation of RxJava & Project Reactor. Both libraries don't allow null
being used for their generic value A
in Flowable
, Flux
, Mono
, etc.
Analogue, you cannot use null
as an empty signal for generic code in Kotlin. Unless A
is constraint to be non-null by using A : Any
.
Only in both cases should you use Arrow's Option, otherwise using Kotlin's nullable type is recommended by the Arrow maintainers.