3 ms·
> was never intended to be a general-purpose Maybe type Say how a general-purpose Maybe type in Java would be different than Optional. "We made a vehicle with
by paulddraper 4y ago
> was never intended to be a general-purpose Maybe type
Say how a general-purpose Maybe type in Java would be different than Optional.
"We made a vehicle with an engine and four wheels but never intended it to be a car."
- layer8 4y agoYou wouldn’t have both of: Optional<T> a = null; Optional<T> b = Optional.empty(); In other words, Optional would have to be a non-nullable type. Which of course means that, to have it be a reference type, Java would have to support non-nullable reference types. But if Java did support those, you wouldn’t really need Optional in the first place, because then the current nullable types would fulfill that purpose.
- vanjajaja1 4y agoThe leaky null abstraction in java is orthogonal to "what does an Optional type express".
- kaba0 4y agoBut the language has another often-used idiom to express a missing value, null. That’s why this was not intended for usage everywhere.
- layer8 4y agoNo, it’s not orthogonal. In Java, all reference types are implicitly optional. The explicit Optional type is only intended as a stop-gap in situations where the implicit version is especially error-prone. But in general it doesn’t make sense to have both the implicit and explicit solution, in particular when the explicit Optionals simultaneously also have the implicit optionality on top. It’s exactly the presence of the implicit optionality that makes Optional nonviable as a general-purpose optionality mechanism (in addition to its performance implications).
- vanjajaja1 4y agoIf you use the implicit optional type then you have no way to express 'not optional'. If you use the explicit optional then you can express optional/not-optional but you're stuck with a potential 3rd state. One of these problems can be fixed by convention (don't use null, ever) and the other has no fix. (unless you go down the path of annotations)