Bug 3483 – Eliminate read-modify-write operations for enums

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-11-06T22:16:00Z
Last change time
2015-06-09T05:15:04Z
Keywords
accepts-invalid, patch
Assigned to
nobody
Creator
andrei

Comments

Comment #0 by andrei — 2009-11-06T22:16:03Z
Consider: enum E { a = 5, b = 10, c = 15; } Arithmetic involving an E are accepted, e.g. E.a + 42, and that's fine because they yield type int which is not convertible back to E. The problem is that read-modify-write expressions are allowed too: E.a x; x += 42; Such operations are nonsensical because they take an enum value easily in places that have nothing to do with the actual defined values. All operations <op>= and also ++ and -- should be disabled for enums.
Comment #1 by smjg — 2009-11-08T06:51:35Z
This is going to break uses of enums as bit flags, which is a valid use according to the docs. Maybe we need a separate "bitflags" type, which would differ in this and other respects....
Comment #2 by smjg — 2009-11-20T14:55:18Z
Moreover, "next value" is a perfectly valid concept for enums, so I don't get why you want ++/-- removed at all.
Comment #3 by andrei — 2009-11-20T16:54:22Z
(In reply to comment #2) > Moreover, "next value" is a perfectly valid concept for enums, so I don't get > why you want ++/-- removed at all. It would be just too odd and requiring a lot of complication to define properly. enum E { a = 5, b = 8, c = 15; } Increment of an enum really becomes a series of if/else statements (if it's 5 make it 8 etc.) or would just increment the actual value leading to values that are not part of the enum. What to do? Then what do you do when you are at the end of the scale and want to increment? Answers could be found, the problem is that reasonable people may think different answers are best, and expect different behaviors.
Comment #4 by yebblies — 2011-06-29T15:18:55Z
Comment #5 by bugzilla — 2011-10-09T12:59:43Z
I'm going to reject this one. I'm not convinced it is a significant source of bugs, there are many legitimate uses of op= for enums, and the rather loose definition of them is traditional for C.
Comment #6 by bearophile_hugs — 2011-10-09T13:29:45Z
(In reply to comment #5) > I'm going to reject this one. I'm not convinced it is a significant source of > bugs, there are many legitimate uses of op= for enums, and the rather loose > definition of them is traditional for C. Time ago I have suggested a @flags attribute to be used on enums that you want to use as bit fields. I vaguely agree with Andrei...