Proposing std.typecons : Optional (with PR) + rant

Johannes Loher johannes.loher at fg4f.de
Tue Jun 11 23:06:52 UTC 2019


Am 11.06.19 um 20:01 schrieb Nick Treleaven:
>> Making a class type optional just means that you're adding *another*
>> type that also means "absent" at a different level of abstraction.
> 
> Re-use null to mean absent.

Java has the exact same problem with all non primitive types being
nullable. They also solved it on the library level with Optional. In
Java, you can never extract null from an Optional:

If you try to constrcut an Optional from null using Optional.of, you
will get an exception and if you use Optional.ofNullable, the result
will simply be an empty Optional (which technically probably holds the
null reference inside, but you can never retrieve it because trying to
access it will throw an exception).

This approach seems to have worked out quite well for Java, so I suggest
we try a similar approach.

Of course the situation is a bit easier for Java than for D because they
only need to care about class types and we have to make everything work
for pointers, arrays, associstive arrays, ..., as well, and the desired
semantics are not that clear for those... But at least for classes, I
think the route that Java took is the correct one.


More information about the Digitalmars-d mailing list