Rust, Swift, Guava, etc. all get this right. Option types need to become a language feature for all statically-typed languages. C++ should adopt it too.
Rust, Swift, Guava, etc. all get this right. Option types need to become a language feature for all statically-typed languages. C++ should adopt it too.
They aren't zero-cost abstractions in most languages. Every unwrap() potentially results in a dynamic check, even if one isn't necessary. There are a number of ways you could extend a type system to track the potential cases of sum types, which would help alleviate this cost.
I contest. The reason for the condition need not be only to indicate whether the other value isn't null; if it was, you could just remove the condition and check nullity. In fact, unless there is either a case where the condition is true and the pointer is null or a case where the condition is false and the pointer is non-null, the condition is useless because checking the pointer would've been enough.
Consider the following silly example, where we have a function
f(Conductor *c, bool isDriving, Train *t)
with the guarantees that c != NULL, and
isDriving==true => t!=NULL. Note that the case isDriving==false does not imply t==NULL; indeed, c could just be waiting for the signal to start driving. enum ConductorTask {
Nothing,
Waiting(Train),
Driving(Train)
}
fn f(c: Conductor, task: ConductorTask)
I think this says everything that your text says, but in a way the compiler understands and can assist the programmer with.Alternatively, since isDriving only makes sense when there's a train, t could be Option<(bool, Train)>, i.e. an optional train along with a boolean for if it's being driven or not.
If it can't, then the version without the check is potentially unsafe, and the check is not unnecessary.
Another reason why exceptions aren't zero-cost in practice is that compilers need to model the exceptional control-flow, and they usualy don't do a great job in this situation and miss a lot of potential optimizations.
Ask me about using a Java library that could return an option type, which could be null, so you have to check for both the None return value AS WELL AS null.
https://wiki.openjdk.java.net/display/valhalla/Minimal+Value...
Until then it is an issue that we have to live with.
Just one example: Java's type erasure trick does not work anymore when value types are type parameters, so it affects the build process. The current approach seems to be to construct specific instances at run time (or rather at class loader time). That is very late though, because some information (like return types) are lost after compilation.
The ongoing changes related to value types, AOT compilation, and overall mechanical sympathy are driven from pressure in the Fintech industry. Which has been moving into Java during the last decade, and currently is eyeing other stacks that could given them the benefits of Java alongside those features, like e.g. Pony.
So of course Oracle wants Java to stay relevant in those domains.
You are required to annotate type parameters for the old reference behavior, currently that would be with the any modifier.
Something like
class Data<any T> {
}No, you don't -- because those are two different kinds of null!
If your library's function returns Optional.empty(), then it's because it successfully returned no value. If it returns null, though, then the library has a bug and you should crash instead of trying to continue in a known invalid state.
Without the Optional type signature, the function would return null for both of these, and your program will appear to work even though it's just suffered a bug.
I'm not sure I agree with that. See how MS did that in C# 7:
https://www.kenneth-truyers.net/2016/01/25/new-features-in-c...
They apparently got the memo: http://en.cppreference.com/w/cpp/utility/optional
Lack of good sugar for pattern matching sometimes makes its use a bit awkward though.