/+ dub.sdl:
name "app"
dependency "mir-core" version="1.1.54"
+/
version = short_visit;
import mir.algebraic;
import std.stdio;
enum Bool : bool { false_ = false, true_ = true }
alias Value = TaggedVariant!(
["str", "raw", "bit", "i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64", "f32", "f64"],
string, const(void)[], Bool, byte, ubyte, short, ushort, int, uint, long, ulong, float, double,
);
void foo(in Value val)
{
version (visit)
{
val.visit!(
(string v) { writeln("str: ", v); },
(const(void)[] v) { writeln("raw: ", v); },
(Bool v) { writeln("bit: ", cast(bool)v ? "true" : "false"); },
(float v) { writefln("f32: %.8f", v); },
(double v) { writefln("f64: %.15f", v); },
(v) { writeln("int: ", v); }, // int's
);
}
else version(full_visit)
{
val.visit!(
(string v) { writeln("str: ", v); },
(const(void)[] v) { writeln("raw: ", v); },
(Bool v) { writeln("bit: ", cast(bool)v ? "true" : "false"); },
(float v) { writefln("f32: %.8f", v); },
(double v) { writefln("f64: %.15f", v); },
(byte v) { writeln("int: ", v); },
(ubyte v) { writeln("int: ", v); },
(short v) { writeln("int: ", v); },
(ushort v) { writeln("int: ", v); },
(int v) { writeln("int: ", v); },
(uint v) { writeln("int: ", v); },
(long v) { writeln("int: ", v); },
(ulong v) { writeln("int: ", v); },
);
}
else version (short_visit)
{
val.visit!(v => writefln("%s: %s", val.kind, v));
}
else
{
template TypeByKind(T, alias kind) if (isTaggedVariant!T && is(typeof(kind) == T.Kind))
{
import std : staticIndexOf, EnumMembers;
alias TypeByKind = T.AllowedTypes[staticIndexOf!(kind, EnumMembers!(T.Kind))];
}
FS: final switch (val.kind) with (Value.Kind)
{
case str: writeln("str: ", val.get!string); break;
case raw: writeln("raw: ", val.get!(const(void)[])); break;
case bit: writeln("bit: ", cast(bool)(val.get!Bool) ? "true" : "false"); break;
case f32: writefln("f32: %.8f", val.get!float); break;
case f64: writefln("f64: %.15f", val.get!double); break;
static foreach (i, k; [i8, u8, i16, u16, i32, u32, i64, u64])
{
case k: writeln("int: ", val.get!(TypeByKind!(Value,k))); break FS;
}
}
}
}
void main() { foo(Value(10)); }
path/to/mir/algebraic.d(2515,33): Error: static assert: "const(Algebraic!(...types...)): the visitor cann't be caled with arguments (byte)"
but it have last "default" function without type specification for argument what can be instanced for all int types.