5 ms·
This is where `Option` or `Optional` types shine. They force you to unwrap the nilable value at compile time, and are zero-cost abstractions. It's not difficult
by echelon 9y ago
This is where `Option` or `Optional` types shine. They force you to unwrap the nilable value at compile time, and are zero-cost abstractions. It's not difficult to use option types, either--especially if the language features syntactic sugar to promote their ergonomics.
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.
- Arcsech 9y agoWhile I love option types, retrofitting them into languages which already have nulls has problems. 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.
- catnaroek 9y agoRetrofitting anything into anything always results in warts. Moral of the story: design things right, right from the beginning.
- tomsmeding 9y ago... which is practically impossible with larger projects that you can't oversee in a glance.
- catnaroek 9y agoSo don't bite more than you can chew?
- pjmlp 9y agoThe situation is known and will be fixed when the JVM gets value types, with minimal value types already on the horizon. https://wiki.openjdk.java.net/display/valhalla/Minimal+Value+Types https://wiki.openjdk.java.net/display/valhalla/Minimal+Value... Until then it is an issue that we have to live with.
- qznc 9y agoWhen or if? I heard one talk about Valhalla some time ago. If this project succeeds it will either be a miracle of engineering or a nightmare to use. 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.
- pjmlp 9y agoWhen. 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> { }
- lmm 9y agoThat's a problem you already have in Java with any other type though. If you have a List or a String or whatever it could always be null.
- cytzol 9y ago> 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. 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.
- Const-me 9y ago> retrofitting them into languages which already have nulls has problems 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-sharp-7-part-2/ https://www.kenneth-truyers.net/2016/01/25/new-features-in-c...
- jacobparker 9y agoFYI that didn't ship in C# 7 More info: https://github.com/dotnet/csharplang/issues/36 https://github.com/dotnet/csharplang/issues/36
- cwzwarich 9y ago> This is where `Option` or `Optional` types shine. They force you to unwrap the nilable value at compile time, and are zero-cost abstractions. 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.
- webkike 9y agoOption types for references need be no more expensive than a null pointer check, and I believe this is how they are implemented in Rust. If a dynamic check is not necessary, then you simply do not use an Option type.
- cwzwarich 9y agoIn C/C++, you quite often end up in situations when some other condition implies that a pointer is nonnull. In Rust, the equivalent code with Option would require an unnecessary dynamic check.
- webkike 9y agoThat's just a problem with ordering. If a condition implies a non null pointer then a non null pointer can imply the condition as well. This is true unless the condition being false does not guarantee a null pointer, in which case the condition is not useful. If some condition implies multiple non null pointers then the option type can be defined on a tuple of references
- tomsmeding 9y ago> in which case the condition is not useful 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.
- deleted 9y ago[deleted]
- klibertp 9y ago> C++ should adopt it too. They apparently got the memo: http://en.cppreference.com/w/cpp/utility/optional http://en.cppreference.com/w/cpp/utility/optional
- nicolapede 9y agoFor people using boost, this feature has been available for some time: http://www.boost.org/doc/libs/1_60_0/libs/optional/doc/html/index.html http://www.boost.org/doc/libs/1_60_0/libs/optional/doc/html/...
- gpderetta 9y agostd::optional is available in C++17. It has also been available in boost for a veeery long time. Lack of good sugar for pattern matching sometimes makes its use a bit awkward though.
- pjmlp 9y agoA new proposal is bubbling up. https://www.youtube.com/watch?v=HaZ1UQXnuC8 https://www.youtube.com/watch?v=HaZ1UQXnuC8