You really need first class support for optionals, a la TypeScript and Kotlin, to get a benefit IMO.
You really need first class support for optionals, a la TypeScript and Kotlin, to get a benefit IMO.
That's why you're supposed to use Optional<Optional<X>>. ;-)
The great thing about Rust is that `Option` is a normal, generic type. You could very easily implement your own equivalent type.
So it can optimize the memory overhead of Option<> away completely.
I've found I agree with the principle that Optionals only make sense for return values (and if the method returns null, that should be treated as a bug).
I've seen Optionals used for member variables, and they might as well just be null. (And are highlighted as such by IDEs like IntelliJ).
- it’s verbose because handling optional values safely is complex
I mean, no shit. But the problem is that tons of existing Java APIs do use nulls, so you can't just pretend they don't exist, and importantly a major point of the optional construct is to get compile-time guarantees, and the Optional class doesn't give you that.
> it’s verbose because handling optional values safely is complex
No, it's not. Kotlin has optionals built into the language, and it's safer and much less verbose.
Their recent decision to axe typeclasses in favor of multiple function receivers, which solves a small subset of problems that HKTs do in Scala and Haskell, make me look for greener pastures whenever I need them.
Java On its own it doesn’t support this however there are ways of enforcing this in a Java codebase. Eg: Errorprone and NullAway comes to mind here.