xxxxxxxxxx
/+dub.sdl:
dependency "sumtype" version="~>0.5"
+/
template acceptOnly(Args...)
if (Args.length >= 2)
{
alias fun = Args[$ - 1];
alias Types = Args[0 .. $ - 1];
auto acceptOnly(T)(T arg)
{
import std.meta: staticIndexOf;
static assert(staticIndexOf!(T, Types) >= 0,
"Invalid argument type `" ~ T ~ "`; must be one of: " ~
Types.stringof);
return fun(arg);
}
}
void main()
{
import sumtype;
import std.stdio: writeln;
SumType!(int, float, string) x = 123;
x.match!(
acceptOnly!(int, float,
num => writeln("number")
),
(string _) => writeln("string")
);
}