Structural exhaustive matching
weaselcat via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 21 23:22:54 PDT 2015
On Wednesday, 22 April 2015 at 04:54:39 UTC, Martin Nowak wrote:
> On Tuesday, 21 April 2015 at 15:36:28 UTC, Jadbox wrote:
>> What's the best equivalent to Rust's structural enum/pattern
>> (match)ing? Is it also possible to enforce exhaustive matches?
>> Basically, I'm curious on what the best way to do ADTs in D.
>
> If it needs to be really fast, use final switch on the tag of a
> discriminated union.
>
> enum Tag { A, B, C }
> struct Val
> {
> Tag tag;
> union
> {
> A a;
> B b;
> C c;
> }
> }
>
> void too(Val val)
> {
> final switch (val.tag)
> {
> case Tag.A: writeln(val.a); break;
> case Tag.B: writeln(val.b); break;
> case Tag.C: writeln(val.c); break;
> }
> }
there's no reason this should be faster than Algebraic(restricted
variant) from std.variant, is there? implementation issue?
More information about the Digitalmars-d-learn
mailing list