Can someone explain this?
Adam D. Ruppe
destructionator at gmail.com
Sat Feb 6 15:00:45 UTC 2021
On Saturday, 6 February 2021 at 14:39:38 UTC, Jeff wrote:
> Okay, the above works. But, I'm not sure why?
Phobos's enum conversion always looks at the identifier, whereas
the rest of the language looks at the value. Remember phobos'
writeln forwards to the rest of its conversions just like `to`.
So in the language like
string s = op; // gives the value under language rules
auto s = cast(string) op; // the value under language rules
to!string(op) // goes through the library's conversion functions
and thus gives the name
The idea is the library tries to be more consistent and names are
generally nice:
enum ERROR {
OK = 1,
FILE_NOT_FOUND = 2,
ETC = 3
}
writeln(error); // FILE_NOT_FOUND prolly better than 2 here
string s = "OK";
ERROR e = to!ERROR(s); // looks it up by name
So it is generally nice and then it does it consistently for
string values too.
> So, I'm guessing there's something going on under-the-hood
> using the ~ operator with the enum and I'd like to understand
> what it is.
mixin and ~ follow language rules and thus work on the value.
> Likewise, if there's an easier method of getting the "value of
> enum" I haven't discovered yet, that'd be just as nice to know.
So when you're writelning, do something under language rules
first so Phobos doesn't see the static type. If phobos doesn't
know it is is an enum, it is forced to work with a value too.
Easiest is to just cast it:
writeln(cast(string) op);
More information about the Digitalmars-d-learn
mailing list