Enum flag patterns #4012
-
I often find myself writing similar patterns with flag enums where certain flags take precedence direction switch
{
_ when direction.HasFlag(Direction.Up) => ...,
_ when direction.HasFlag(Direction.Down) => ...,
Direction.Left => ...,
Direction.Right => ...,
}; It would be really convenient to have a direction switch
{
flag Direction.Up => ...,
flag Direction.Down => ...,
flag Direction.Left => ...,
flag Direction.Right => ...,
}; Has there been any discussion on this? I wouldn't mind trying to write a proposal or maybe even attempt an implementation if this is something desirable. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This does seem interesting, but I'm not sure how practically useful it is: With flags, you generally want to handle all flags. In practice in your case for example I would imagine you want to handle all these cases separately: 0
Up
Down
Left
Right
Up Left
Up Right
Down Left
Down Right flags wouldn't be approriate for this, whilst a normal switch on enum values would. |
Beta Was this translation helpful? Give feedback.
-
With #1047 I could image you'd be able to do something like that static bool Flag(this Direction self, Direction d) => self.HasFlags(d); // hypothetical "pattern extensions"
direction switch
{
Flag(Direction.Up) => ...,
Flag(Direction.Down) => ...,
} |
Beta Was this translation helpful? Give feedback.
With #1047 I could image you'd be able to do something like that