[API Proposal]: Add native support for exhaustive enum-based switch expressions #7659
Unanswered
skashtanoff
asked this question in
Language Ideas
Replies: 3 comments
-
This is a C# language proposal, not API proposal. See #3179 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Some additions:
The compiler already generates a branch throws
Currently attributes can only be applied to constructs with compiled form in IL, like methods and fields. It can't be applied to statements inside a method. |
Beta Was this translation helpful? Give feedback.
0 replies
-
See: #3179 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Background and motivation
Now (.NET 7), if we have a
enum
:Then when we use it in
switch
-expression:The compiler will produce a warning/error despite of the fact the pattern is exhaustive.
API Proposal
Any of the following would be acceptable
throw UnreachableException
by default[ExhaustivePattern]
that turns the warning off. For example, we are sure that the value comes to us areWest
orEast
, and never equals toNorth
,South
, how about we tell about that to the compiler? maybe the JIT will manage to produce more optimal code based on this assumptionAPI Usage
Alternative Designs
No response
Risks
backward compatibility
Beta Was this translation helpful? Give feedback.
All reactions