Bug 10965 – Allow alias initializer as enum member to avoid counter reset
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-04T17:47:00Z
Last change time
2013-09-08T18:47:54Z
Keywords
pull
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2013-09-04T17:47:00Z
I've recently ran into a hard to track bug in my code:
-----
enum MouseAction
{
press,
release,
/** Convenience - equal to $(D press). */
click = press,
double_click, // oops, it's now equal to release!!
}
void main()
{
static assert(MouseAction.release != MouseAction.double_click); // fail
}
-----
Unfortunately the introduction of the convenience member ended up re-setting the enum member init counter, which ended up making "double_click" equal the value of "release".
To avoid such buggy code, but still allow these convenience members, I propose we introduce member aliases as a new feature:
-----
enum MouseAction
{
press,
release,
alias click = press, // same as press, but does not reset the counter!
double_click, // equals release + 1
}
void main()
{
static assert(MouseAction.release != MouseAction.double_click); // ok
}
-----