Overriding to!string on enum types
evilrat via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Sep 2 07:59:39 PDT 2014
sorry, i forgot everything.
here is example of how to do this
-----
import std.conv : to;
enum Test
{
One,
Two,
Three,
}
template to(T: string)
{
T to(A: Test)(A val)
{
final switch (val)
{
case Test.One: return "1";
case Test.Two: return "2";
case Test.Three: return "3";
}
}
}
void main()
{
assert(to!string(Test.One) == "1");
auto t = cast(Test)2;
assert(to!string(t) == "3");
assert(to!int("4") == 4); // shows original to! template works
}
More information about the Digitalmars-d-learn
mailing list