I've read a few posts on Java 8's upcoming Optional type, and I'm trying to understand why people keep suggesting it's not as powerful as Scala's Option. As far as I can tell it has:
What am I missing?
Some possibilities come to mind (OTOH, I haven't seen people actually saying that, so they might mean something else):
No pattern matching.
No equivalent to Scala's fold
or Haskell's fromMaybe
: you have to do optional.map(...).orElseGet(...)
instead.
No monadic syntax.
I also wouldn't call any of these "less powerful" myself, since you can express everything you can with the corresponding Scala/Haskell types; these are all conciseness/usability concerns.
Optional
and Maybe
are effectively in correspondence. Scala has None
and Some[A]
as subclassing Option[A]
which might be more directly comparable to Java since Java could have done the same.
Most other differences either have to do with the ease of handling Maybe
/Option
in Haskell/Scala which won't translate since Java is less expressive as a language or the consistency of use of Maybe
/Option
in Haskell/Scala where many of the guarantees and conveniences afforded by the type only kick in once most libraries have agreed to use optional types instead of null
s or exceptions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With