print enum value rather name from enum X : string

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Feb 13 02:05:32 UTC 2018


On Tuesday, February 13, 2018 01:55:59 Marc via Digitalmars-d-learn wrote:
> 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.

Well, those are generally referred to as manifest constants rather than
enums (though the spec calls them anonymous enums), and they're
fundamentally different from enums in the sense that they aren't grouped
together and don't create a type. If all you want is a bunch of constants,
and you're not planning on doing something like having a function take a
variable of an enum type, then it generally makes more sense to use manifest
constants than enums, but if you want to actually treat the enum as a type,
then you need an actual enum. So, really, it comes down to what you're
trying to do.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list