Please add support for bitwise shift operators on enums #849
Replies: 6 comments
-
I think the casting is a good thing because it alerts people to the fact that you're doing something which is generally unintuitive and difficult to reason about. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 |
Beta Was this translation helpful? Give feedback.
-
@weitzhandler I'm not downvoting, but that still doesn't change my mind though. I'll let others weigh in. |
Beta Was this translation helpful? Give feedback.
-
@weitzhandler I wouldn't say the proposal doesn't make sense but I think that at least in this case, it makes more sense to provide a thin wrapper over the enum and add methods that make sense for it such as |
Beta Was this translation helpful? Give feedback.
-
I use extension methods but I'm also excited about #297. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Totally. ;) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
In
[Flags]
annotated enums, I often want to move an enum property to the next value, something like the++
operator, but for enum.What I want, is to be able to move the property value to next day:
Example usage
Current behavior
Currently it's achievable like this:
My suggestion is to add
<<
and>>
bitwise shift operators toEnum
, so that we can benefit from this without casting.Notes
Maybe the
DaysInWeek
example I gave here isn't the right one to demonstrate the motive so well. Just imagine you have an enum that is in some cases supposed to be a single value and you want to advance it to next value. And BTW, a multi-value enum will move to the next set of values (i.e. if it's set toSunday,Monday
, shifting by one will set it toMonday,Tuesday
.Additionally, take into account that enum supports all
int
operations, including++
or--
, I think it should be just natural to also enable the bitwise shift operators to complete the picture.Initially posted here: https://github.com/dotnet/corefx/issues/23524.
Beta Was this translation helpful? Give feedback.
All reactions