Structural exhaustive matching
Martin Nowak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 21 21:54:37 PDT 2015
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;
}
}
More information about the Digitalmars-d-learn
mailing list