print enum value rather name from enum X : string
Marc
jckj33 at gmail.com
Tue Feb 13 01:55:59 UTC 2018
On Monday, 12 February 2018 at 17:29:47 UTC, Jonathan M Davis
wrote:
> On Monday, February 12, 2018 17:07:50 Marc via
> Digitalmars-d-learn wrote:
>> [...]
>
> If you actually use the enum values anywhere other than with
> anything from std.conv, std.format, or std.stdio, then when
> they get converted to strings, you get their actual values.
> It's just that those modules specifically grab the names of the
> enum members when converting enums to strings, since in all
> cases other than with strings, it's generally desirable that
> when converting an enum member to string, you get the name -
> and with enums with a base type of string, whether you want the
> name or the value depends on what you're trying to do. Both can
> be useful.
>
> So, when dealing with std.format, std.conv, and std.stdio, if
> you want an enum of base type string to be treated as a string,
> then you'll have to force it with a cast. Anywhere else, if
> they get converted to string, then you'll get their values. If
> that is unacceptable for your use case for whatever reason,
> then you'll have to try a different solution. What solution
> would then work best would presumably depend on whatever it is
> you're actually trying to do.
>
> - Jonathan M Davis
Thanks for you always well-thought-out answer. I was going to
print it with writefln() calls more than anywhere else so to
avoid casts in all those places, which would make it ugly, I just
used
> enum foo = "a";
> enum baa = "b";
which I found to be more common D-idiomatic.
More information about the Digitalmars-d-learn
mailing list