How to migrate from Java 8 `Optional` to Kotlin's ...
# feed
d
How to migrate from Java 8
Optional
to Kotlin's null-safety features. https://typealias.com/guides/java-optionals-and-kotlin-nulls/
👍 2
a
There might still be one case when you’d use an Optional in Kotlin
Second case is when API you’re working with doesn’t support null values, like RxJava 2 Plus you can add that Kotlin nullable saves an allocation in compare to Optional in exchange to != null checks added by compiler (although Optional needs to be dereferenced, so Kotlin nullable should win overall) Other than that — really good migration guide 👍
r
In case anyone is interested in a safe Option type in Kotlin there is also http://kategory.io/docs/datatypes/option/
👍 2
contains no unsafe APIs and monad comprehension among other features
a
Or a more minimalistic one
<https://github.com/gojuno/koptional>
👏 1
d
@artem_zin and @raulraja - Thanks for your comments! I haven't looked at Kategory's Option or koptional, so I'm going to go check 'em out. Do you all mind if I incorporate your feedback into the article?
a
@Dave Leeds sure, go ahead 🙂
r
@artem_zin please and thank you for your article!
It's already is helpful to people wondering about the differences. I learnt new stuff reading it.
a
I guess you meant djleeds 😄
o
By the way, it is possible to define all the Optional methods on nullable as extension methods. Then change Option<Type> to Type? and fix all the compile errors with alt+enter. Then inline these extension methods. You are done with the migration. I'm not 100% sure it will work with all Optional's methods, but it worked on my previous project very well.
r
@artem_zin yeah sorry I ment to mention @Dave Leeds xD
👍 1
🙂 1
d
@Oleksii That's a clever idea! I'll have to give that a try.