Phobos' std.conv.to-conversion from enum to string doesn't scale beyond hundreds of enumerators
Per Nordlöw
per.nordlow at gmail.com
Sun Jun 24 21:47:14 UTC 2018
On Sunday, 24 June 2018 at 17:23:54 UTC, Steven Schveighoffer
wrote:
> static if(__traits(compiles, fastEnumToString(val)))
> return fastEnumToString(val);
> else
> return slowEnumToString(val); // checks for duplicates
>
> Should eliminate the issues, because it's not going to compile
> the slow version if the fast version can work.
>
> -Steve
Yes, I thought about that too, but the problem is that
std.conv.to is used in std.stdio and I don't want to remember to
always to do
writeln("Some text:", x.to!string);
instead of
writeln("Some text:", x);
for some enum instance `x`.
I'm gonna hack up another solution
struct Enum(E)
if (is(E == enum))
{
@property string toString() @safe pure nothrow @nogc
{
// fast implementation
}
E _enum;
alias _enum this;
}
Further, it just struck me that we can generalize my fast
solution to include enumerations with enumerator aliases that are
defined directly after its original enumerator by checking with
a `static if` if the current enumerator value equals the previous
then we skip it. I'm gonna post the solution here after some
hacking.
More information about the Digitalmars-d
mailing list