Algebraic data types
Jonathan via Digitalmars-d
digitalmars-d at puremagic.com
Mon Oct 6 09:53:51 PDT 2014
NM, I found this:
http://www.digitalmars.com/d/archives/digitalmars/D/Algebraic_Data_Types_in_D_239039.html
"D's Algebraic needs some work, but it's okay for basic usage."
+1 agree
----
import std.stdio;
import std.variant;
struct Red {}
struct Green{}
struct Blue {}
struct RGB
{
int r;
int g;
int b;
}
alias Color = Algebraic!(Red, Green, Blue, RGB);
void main()
{
auto r = Color(RGB(64, 128, 255));
r.visit!(
(Red r) => writeln("Red"),
(Green g) => writeln("Green"),
(Blue b) => writeln("Blue"),
(RGB rgb) => writefln("RGB(%s, %s, %s)", rgb.r, rgb.g, rgb.b),
);
}
More information about the Digitalmars-d
mailing list