On 7/31/14, 5:35 PM, Timon Gehr wrote:
On 07/31/2014 06:23 PM, Andrei Alexandrescu wrote:
On 7/31/14, 6:03 AM, w0rp wrote:
On Thursday, 31 July 2014 at 11:42:21 UTC, Remo wrote:
http://tech.esper.com/2014/07/30/algebraic-data-types/
D already has product type it is struct.
But D lacks sum type also called tagged-union.
Do you think it would be possible to add something like this to D2 ?
There is a library solution for this in the standard library.
http://dlang.org/phobos/std_variant.html#.Algebraic
It doesn't handle recursive types at the moment, like alias Foo =
Algebraic!(Foo[]). Apart from that, it should be what you are looking
for.
alias Foo = Algebraic!(This[]);
Andrei
alias Foo = Algebraic!(int,Algebraic!(Foo[],double)[]);
Good point! That's why I'd submitted this at some point:
https://issues.dlang.org/show_bug.cgi?id=9608
It should be possible to achieve alpha replacement of This with the
needed type at any level inside a composite type.
There is also this kind of approach:
mixin ADT!q{
List(T):
| Nil
| Cons T List!T
};
Interesting! Is there code available for that?
Of course, product types ("tuples") and sum types ("tagged unions") and
recursive types are elementary enough to be proper language features in
one way or another with all the syntactic convenience that yields for
pattern matching.
Well one other way to look at it is that striving to do things in a
library pushes introspection forward.
I do agree tagged unions should be pushed into the language; they'd help
the GC.
Andrei