Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Add native support for exhaustive enum-based switch expressions #7658

Closed
skashtanoff opened this issue Oct 13, 2023 · 2 comments

Comments

@skashtanoff
Copy link

Background and motivation

Now (.NET 7), if we have a enum:

enum State
{
    Off,
    On
}

Then when we use it in switch-expression:

var nextState = currentState switch
{
    State.Off => State.On,
    State.On  => State.Off 
};

The compiler will produce a warning/error despite of the fact the pattern is exhaustive.

API Proposal

Any of the following would be acceptable

  • allow the compiler to generate the default branch if the pattern is exhaustive. for example it could be throw UnreachableException by default
  • add an attribute like [ExhaustivePattern] that turns the warning off. For example, we are sure that the value comes to us are West or East, and never equals to North, South, how about we tell about that to the compiler? maybe the JIT will manage to produce more optimal code based on this assumption

API Usage

// no warning here
var nextState = currentState switch
{
    State.Off => State.On,
    State.On  => State.Off 
};

Alternative Designs

No response

Risks

backward compatibility

@ghost ghost added the untriaged label Oct 13, 2023
@skashtanoff skashtanoff changed the title [API Proposal]: Add native support for exhaustive enu-based switch expressions [API Proposal]: Add native support for exhaustive enum-based switch expressions Oct 13, 2023
@huoyaoyuan
Copy link
Member

This is a C# language proposal, not API proposal. See #3179

@huoyaoyuan huoyaoyuan closed this as not planned Won't fix, can't repro, duplicate, stale Oct 13, 2023
@ghost ghost removed the untriaged label Oct 13, 2023
@huoyaoyuan
Copy link
Member

Some additions:

  • allow the compiler to generate the default branch if the pattern is exhaustive. for example it could be throw UnreachableException by default

The compiler already generates a branch throws SwitchExpressionException, with a warning about it.

  • add an attribute like [ExhaustivePattern] that turns the warning off.

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.

@stephentoub stephentoub transferred this issue from dotnet/runtime Nov 6, 2023
@dotnet dotnet locked and limited conversation to collaborators Nov 6, 2023
@stephentoub stephentoub converted this issue into discussion #7659 Nov 6, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants