Type unions in D

Edward Diener eddielee_no_spam_here at tropicsoft.com
Tue Sep 15 21:16:38 PDT 2009


Justin Johansson wrote:
> Jeremie Pelletier Wrote:
> 
>> As for the max of an empty set, you can just return zero.
> 
> Well you could return -1, 99 or any arbitrary integer for that matter; just that it doesn't make sense to do that from a pure maths perspective.  Mathematicians have spent centuries trying to make number systems consistent.  The invention (or discovery**) of "i", the square-root of minus-one, was a major major break-through.  In similar vein, it's nice if the type systems we use for programming follow from a consistent set of axioms. ;-)

Your return situation is what an 'optional' template is made for. The 
entire idea about 'optional' is that you should be able to always return 
a value for a type which is not a valid value for that type. Some other 
languages use a built-in value, like 'None' in Python, to solve the 
problem. C++ and D can use an 'optional' template to solve this problem. 
See optional<T> in Boost for the C++ answer. I believe Andrei is working 
on a similar solution for D.



More information about the Digitalmars-d mailing list