Variant

Variant Type (aka Algebraic Type).

The impllementation is defined as

alias Variant(T...) = Algebraic!(TypeSet!T);

Compatible with BetterC mode.

alias Variant(T...) = Algebraic!(TypeSet!T)

Examples

Variant!(int, double, string) v = 5;
assert(v.get!int == 5);
v = 3.14;
assert(v == 3.14);
// auto x = v.get!long; // won't compile, type long not allowed
// v = '1'; // won't compile, type char not allowed

Single argument Variant

static struct S
{
    int n;
    this(ref return scope inout S rhs) inout
    {
        this.n = rhs.n + 1;
    }
}

Variant!S a = S();
auto b = a;

import mir.conv;
assert(a.get!S.n == 0);
assert(b.n == 1); //direct access of a member in case of all algebraic types has this member

Empty type set

Variant!() a;
auto b = a;
assert(a.toHash == 0);
assert(a == b);
assert(a <= b && b >= a);
static assert(typeof(a).sizeof == 1);

Small types

struct S { ubyte d; }
static assert(Nullable!(byte, char, S).sizeof == 2);

Meta