Java is well on its way to alleviating the problem. [1]
Java is well on its way to alleviating the problem. [1]
In Dart[1] and Vlang[2], for example, "non-nullable" is the default. Vlang doesn't allow null or nil to be used, unless code is marked as unsafe, and primarily only for compatibility with languages that do (like C). In Dart, nullable variables have `?` added to the end of them.
[1]: https://en.wikipedia.org/wiki/Dart_(programming_language)
If you have something like
Map<Integer, Optional<Integer>> map;
var foo = map.get(1);
if `1` isn't present in the map then `foo` has to be set to something. It will be `null` in this case.If the new null restricted stuff makes it in, you can express this as
Map<Integer, Optional<Integer>!> map;
var foo = map.get(1);
and yet, `foo` will still end up being `null` in that case (I assume) because something has to come back if `1` isn't present.At best, that will prevent you from doing `map.put(1, null);`
Am I missing something?
To be more clear:
Map<K, V>.get(foo)
returns a `V`, not an `Optional<V>`. If your `V` is `Optional<WrappedV>`, cool, but that doesn't change that `Map` either finds or doesn't find a V. Optional<V> get(K)
sounds like an 'obvious' addition, though also I've found that when something 'obvious' isn't added to Java since 1.7, the explanation proffered makes sense to me.If you had something like:
var x = map.get(k);
The type of `x` could not be inferred.The best they could hope for is a signature like `Optional<V> getOpt(K);`. Which wouldn't be terribly bad as Java currently has an annoying get definition in the `Map` interface because it predates generics (The current signature is `V get(Object)`).
Regardless, if they do want to change the interface, it'd probably be best if they waited for Valhalla to land. One of the problems with `Optional` as it stands is it creates a load of GC pressure. If `Optional` is converted into a value class then there will be effectively no overhead to returning it everywhere.