Overriding to!string on enum types
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Sep 2 08:41:16 PDT 2014
On Tuesday, 2 September 2014 at 12:54:55 UTC, Nordlöw wrote:
> Is it possible to override the behaviour of to!string(x) when x
> is an enum. I'm asking because this
>
> enum CxxRefQualifier
> {
> none,
> normalRef,
> rvalueRef
> }
>
> string toString(CxxRefQualifier refQ) @safe pure nothrow
> {
> final switch (refQ)
> {
> case CxxRefQualifier.none: return "";
> case CxxRefQualifier.normalRef: return "&";
> case CxxRefQualifier.rvalueRef: return "&&";
> }
> }
>
> doesn't affect behaviour of to!string(x) when x is an instance
> of CxxRefQualifier.
That won't work, because your "toString" is a free function. The
module system doesn't allow this kind of "Koenig lookup"-like
hijack. The only reason it allows for things like front/popFront
and arrays, is that the other modules import std.array, and are
"aware" of the functions. This is not true for user defined
types. This may or may not be a feature :)
Unless we allow defining "enum-member functions", AFAIK, it is
impossible to override the printing behavior for enums... short
of injecting your own modules in std.format/std.conv. Or to have
a parameter "moduleLookup" in said template functions.
More information about the Digitalmars-d-learn
mailing list