Passing anonymous enums as function parameters

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Dec 17 19:45:31 UTC 2017


On Sunday, December 17, 2017 12:47:26 kerdemdemir via Digitalmars-d-learn 
wrote:
> What I meant with anonymous enums was:
> https://dlang.org/spec/enum.html#anonymous_enums. Maybe I
> couldn't explain well but I believe D have anonymous enums. I am
> sorry I have forgotten to remove " :string" in my example from
> the "enum : string". Please stretch out ": string" part my
> problem is not related with that.

That's pretty much just declaring manifest constants with braces so that you
don't repeat the keyword enum a bunch of times.

enum
{
    a = "foo",
    b = "bar",
    c = "baz";
}

is identical to

enum a = "foo";
enum b = "bar";
enum c = "baz";

and it will even let you mix types, e.g.

enum
{
    a = "foo",
    b = 42
}

which you can't do with actual enums. You're not declaring a new type.
You're just declaring a bunch of constants. They're really not enums in the
classic sense, and for the most part, folks around here aren't going to call
them enums. If anything, a number of folks complain that the keyword enum
was reused for manifest constants. I don't know why the documentation has a
section for "anonymous enums" separate from manifest constants, since
they're really not a different thing, and AFAIK, pretty much no one calls
them that - though at least they're right next to the documentation for
manifest constants.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list