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()
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()
Copyright © 2021 Jogjafile Inc.
There is no such function at the moment, but such a contribution would be welcomed!
Arrow however does not recommend using
Optionunless absolutely necessary. The only use-case being nested nulls, which is the limitation for ReactiveX implementation of RxJava & Project Reactor. Both libraries don't allownullbeing used for their generic valueAinFlowable,Flux,Mono, etc.Analogue, you cannot use
nullas an empty signal for generic code in Kotlin. UnlessAis constraint to be non-null by usingA : Any.Only in both cases should you use Arrow's Option, otherwise using Kotlin's nullable type is recommended by the Arrow maintainers.