At what point do you decide on using an Option ove...
# arrow
k
At what point do you decide on using an Option over a nullable type?
t
I think it was there for some java support, but within kotlin I would always use nullability
s
TL;DR nested nullability, and it's always the same problem. Java inherently has this problem, and often results in false negatives as well. All generics from Java are constraint to
Any?
, not
Any
so everything generic is nullable but when inserting a
Int?
you get a nested null. Project Reactor, and/or RxJava, choose to use
null
as an internal optimisation mechanism and as a result they do not allow
null
as a value for
T
. There you have to resort to
Option
or Java's
Optional
to allow such a pattern of absent values. However, in Kotlin (and with KotlinX Flow) this is never really needed. In generic code you can avoid this problem, or work around it. Here is an in detail explanation on the website, https://arrow-kt.io/learn/typed-errors/nullable-and-option/
🙏 1
mind blown 1
gratitude thank you 2
👍 10