Recursive discriminated unions [Phobos2]

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Feb 10 20:41:17 PST 2009


Chris Nicholson-Sauls wrote:
> Okay, we all [hopefully] know what a discriminated union is, and those 
> of us who are trying out D2 can probably generally agree that 
> std.variant.Algebraic makes life a good deal easier in this arena.  The 
> actual usage is different, but it is definitely a gift from heaven. 
> Except there's one thing it can't do: recursive types.
[snip]

Ok, I have a solution now. I'm not checking it in yet because I have a 
brazillion other changes in my tree and I don't want to break something. 
But I attach it to this message and it contains the following unittest:

unittest
{
     alias Algebraic!(real, This[], This[int], This[This]) A;
     A v1, v2, v3;
     v2 = 5.0L;
     v3 = 42.0L;
     v1 = [ v2 ][];
     auto v = v1.peek!(A[]);
     writeln(v[0]);
     v1 = [ 9 : v3 ];
     writeln(v1);
     v1 = [ v3 : v3 ];
     writeln(v1);
}

The patterns This[], This*, This[U], U[This], and This[This] are 
detected, but alas, no general solution yet (I don't know how to). 
Anyhow, std.variant is starting to get interesting with this feature in tow.


Andrei


-------------- next part --------------
A non-text attachment was scrubbed...
Name: variant.d
Type: text/x-dsrc
Size: 39058 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20090210/b68d4f32/attachment.d>


More information about the Digitalmars-d mailing list