Enum alias members: yay or nay?

Jonathan M Davis jmdavisProg at gmx.com
Sun Sep 8 16:54:27 PDT 2013


On Sunday, September 08, 2013 12:08:07 monarch_dodra wrote:
> On Sunday, 8 September 2013 at 09:43:02 UTC, Kenji Hara wrote:
> > I also agree that the compiler enhancement is overkill.
> > 
> > Kenji Hara
> 
> Let's not throw this away quite yet: There is *another*
> fundamental difference:
> 
> enum S
> {
>      a,
>      b = a,
> }
> 
> This creates an enum with *two* entries.
> 
> enum S
> {
>      a,
>      alias b = a,
> }
> 
> This would create an enum with a *single* entry, which can be
> accessed via two different names.
> 
> *This*, in itself, I think is a good idea. It helps distinguish
> between "an enum that has multiple entries, some of which have
> the same values" and "an enum whose entry 'a' can also be refered
> to as 'b', fo rconvenience".
> 
> For starters, the distinction would be self documenting.
> 
> Second, once you involve things like `EnumMembers`, it becomes a
> pretty interesting distinction to make.

But then things get weird, because EnumMembers wouldn't return everything, and 
presumably final switch wouldn't have every member. It's not necessarily a bad 
idea, but I think that it would have to be thought through very thoroughly, 
and ultimately, I'm not sure that it's all that valuable.

The main feature that enums lack that would be nice would be the ability to 
deprecate their members (presumably with the intention of replacing them with 
new names). Aliases of some kind might be beneficial there, but again, I think 
that it all would have to be thought through quite thoroughly. And ultimately, 
it might be that it's just better to deprecate the entire enum at once and 
come up with a new name, much as that's often not what you want to do, because 
you just want to rename some of the values rather than replace the whole 
thing.

So, all in all, I think that any changes to enums along these lines really 
need to be thought through carefully before we consider doing anything, and 
whatever changes we make would have to pull their weight (which Andrej's 
suggestion doesn't do - and he seems to now agree with us on that).

- Jonathan M Davis


More information about the Digitalmars-d mailing list