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.
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....
Moreover, "next value" is a perfectly valid concept for enums, so I don't get why you want ++/-- removed at all.
(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.
Possible solution and phobos changes: https://github.com/D-Programming-Language/dmd/pull/177 https://github.com/D-Programming-Language/phobos/pull/127
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.
(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...