Recursive discriminated unions [Phobos2]

Chris Nicholson-Sauls ibisbasenji at gmail.com
Tue Feb 10 17:29:43 PST 2009


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.


// D1
struct MyVar {
	// ...
	union {
		int i;
		double f;
		char[] s;
		MyVar[] l;	// lists good to go
		MyVar[MyVar] h;	// hashtables good to go
	}
	// ...
}


// D2
alias Algebraic!(int, double, string, MyVar[], MyVar[MyVar]) MyVar;

Bzzzzzt!  Big ole error!


Fooey.  Because I want this, badly (its absolutely necessary for a 
program I'm working on).  I asked Andrei if he had any suggestion, 
especially since it is a known limitation mentioned explicitly in the 
specs.  No luck.  He did have an idea on syntax:

Algebraic!(int, double, string, This[])

Where the 'This' token is recognized specially as recursive.  It reads 
well, although my first thought was '_' similar to the way std.bind 
works.  The million dollar question is: how to make this work?

-- Chris Nicholson-Sauls

PS: Apologies, as I just got home from a very long day, so the brain is 
a bit tired and I probably came off silly.



More information about the Digitalmars-d mailing list