Enum.toString

The inverse of fromString, this function takes an enum member value and returns its string identifier.

It lowers to a big switch of the enum members. It is faster than std.conv.to and generates less template bloat.

Taken from: https://forum.dlang.org/post/bfnwstkafhfgihavtzsz@forum.dlang.org written by Stephan Koch (https://github.com/UplinkCoder). Used with permission.

template Enum(E)
@safe pure
string
toString
()
if (
is(E == enum)
)

Parameters

value E

Enum member whose string name we want.

Return Value

Type: string

The string name of the passed enum member, or (for instance) cast(E)1234 if an invalid value of 1234 was passed, cast to type E.

Examples

enum SomeEnum { one, two, three };

string foo = Enum!SomeEnum.toString(SomeEnum.one);
assert(foo == "one");

See Also