47 ms·
Note that Java’s Optional was never intended to be a general-purpose Maybe type or general-purpose replacement for null, unlike Rust’s option. As Brian Goetz ex
by layer8 4y ago
Note that Java’s Optional was never intended to be a general-purpose Maybe type or general-purpose replacement for null, unlike Rust’s option. As Brian Goetz explains in https://stackoverflow.com/a/26328555/623763 https://stackoverflow.com/a/26328555/623763:
“Our intention was to provide a limited mechanism for library method return types where there needed to be a clear way to represent "no result", and using null for such was overwhelmingly likely to cause errors.
For example, you probably should never use it for something that returns an array of results, or a list of results; instead return an empty array or list. You should almost never use it as a field of something or a method parameter.
I think routinely using it as a return value for getters would definitely be over-use.”
- chriswarbo 4y ago> For example, you probably should never use it for something that returns an array of results, or a list of results; instead return an empty array or list This also applies to null/Maybe as well: both would violate the principle of least surprise (e.g. the AWS DynamoDB SDK has queries return an 'Array<Item>'; but this is 'null' if there are no matches!). It also complicates the domain model, making two distinct forms of empty value ('None' versus 'Some(List())'; or '[]' versus 'null'), which may not have any semantic difference. > You should almost never use it as a field of something I agree, although it's often preferable to expose methods rather than fields anyway; in which case it's a return value, which seems OK. > or a method parameter Sure, that's what polymorphism/overloading is good for, e.g. instead of `foo(int arg1, Optional<String> arg2)` we can have separate `foo(int arg1, String arg2)` and `foo(int arg1)` definitions (where the latter will probably call the former with some default). > I think routinely using it as a return value for getters would definitely be over-use I agree, since that would indicate our model is too weak, and missing some domain-relevant information. For example, if many of our 'Order' methods return optional results, there's probably a finer-grained distinction to be made, like 'PendingOrder', 'FulfilledOrder', etc. which don't need the optional qualifiers. (Personally I try to avoid the term "getter": APIs should make sense without reference to their underlying implementation; whether that happens to be "getting" a field, or calling out to some other methods/objects, etc. That's the point of encapsulation :) )
- 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 ago