SumType

Undocumented in source.

Constructors

this
this(T value)
Undocumented in source.

Destructor

~this
~this()
Undocumented in source.

Postblit

this(this)
this(this)
Undocumented in source.

Members

Aliases

Union
alias Union = UnionT
Undocumented in source.
visit
alias visit(Handlers...) = _visit!(typeof(this), Handlers)
Undocumented in source.

Functions

contains
bool contains()
Undocumented in source. Be warned that the author may not have intended to support it.
get
T get()
Undocumented in source. Be warned that the author may not have intended to support it.
opAssign
void opAssign(T value)
Undocumented in source. Be warned that the author may not have intended to support it.
set
void set(T value)
Undocumented in source. Be warned that the author may not have intended to support it.

Properties

kind
Kind kind [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.

Static functions

kindOf
Kind kindOf()
Undocumented in source. Be warned that the author may not have intended to support it.

Structs

Kind
struct Kind
Undocumented in source.

Examples

union U
{
    int a;
    string b;
}

alias Sum = SumType!U;

static assert(Sum.kindOf!int    == Sum.Kind.a);
static assert(Sum.kindOf!string == Sum.Kind.b);

auto value = Sum(20);
assert(value.kind == Sum.Kind.a && value.kind == Sum.kindOf!int);
assert(value.contains!int);

// bool threw = false;
// try value.get!string();
// catch(Error error)
//     threw = true;
// assert(threw);

assert(value.get!int == 20);
value = "lol";
assert(value.kind == Sum.Kind.b && value.kind == Sum.kindOf!string);
assert(value.contains!string);
assert(value.get!string == "lol");

int unhandled;
void visitTest(ref Sum sum)
{
    sum.visit!(
        (ref int i) { i *= 2; },
        (ref string b) { b = "lel"; },
        () { unhandled++; }
    )(sum);
}

value = 20;
visitTest(value);
assert(value.get!int == 40);

value = "lol";
visitTest(value);
assert(value.get!string == "lel");

// value = new Object();
// visitTest(value);
// assert(unhandled == 1);

Meta