Shameless autopromotion : type safe tagged union in D
deadalnix
deadalnix at gmail.com
Fri May 10 06:53:14 PDT 2013
On Friday, 10 May 2013 at 13:11:37 UTC, Dmitry Olshansky wrote:
> E.g. this is exactly the same switch madness we sought to avoid:
>
The article don't plan to avoid this, but to make the tagged
union safe. I agree this is a plus, but didn't really took time
to make that work.
> void process(T)(T data) {
> alias Type = typeof(data);
>
> static if(is(Type == A)) {
> // Code that handle the case where it is an A.
> } else static if(is(Type == B)) {
> // Code that handle the case where it is an B.
> } else {
> static assert(0, "You must handle type " ~
> Type.stringof);
> }
> }
>
> t.apply!process();
>
>
> I'd rather see another idiom supported too:
>
> t.apply!(processA, processB)();
>
> where e.g.
> void proccessA(A value){ ... }
>
> void processB(B value){ ... }
>
That can work indeed.
> Anther thing is that will allow succinct notations like:
>
> int squared = t.apply!(
> (A a) => a.x*a.x + a.y*a.y,
> (B b) => b.x*b.x + b.y*b.y + b.z*b.z
> )();
>
> I sure don't fancy putting static if into lambdas.
> Another thing about it is it allows specifying safe catch all
> or default case.
That last one won't work. DMD don't accept locals here, even if
they are functions.
More information about the Digitalmars-d-announce
mailing list