Skip to content

Enhance switch expression support for closed classes set (sealed classes/private protected constructor only) #48446

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

Open
rmannibucau opened this issue Apr 14, 2025 · 6 comments
Assignees
Labels
untriaged Request triage from a team member

Comments

@rmannibucau
Copy link

Is your feature request related to a problem? Please describe.

Goal is to avoid the default case when it can't happen

Describe the solution you'd like

Assuming you have a private protected constructor as only way to create an abstract class instance, it means all classes will be defined in the assembly so the compiler know all classes, then switch expression testing the type only shouldn't require default when all types are listed

Additional context

Ex:

public abstract class Base
{
    private protected Base() { }
    public abstract string Whatever();
}

public class Foo : Base { public override string Whatever() => "foo"; }

public class Bar : Base { public override string Whatever() => "bar"; }

public static class Demo
{
    public static void Main(string[] args)
    {
        Base instance = new Foo();
        var test = instance switch
        {
            Foo f => f,
            Bar b => b,
            { } other => other, // this is useless but required today
        };
    }
}
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Request triage from a team member label Apr 14, 2025
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

1 similar comment
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@KalleOlaviNiemitalo
Copy link
Contributor

Can't the program use the reflection emit API to create another derived class?

@rmannibucau
Copy link
Author

@KalleOlaviNiemitalo assuming so, the fact is a standard switch doesn't need that and the program would need to modify the switch anyway to handle the class so compiler really assume build time code and runtime code must ensure it modifies the code properly to keep it working, default case in switch expression is not in this case most of the time for previous case since you designed a closed set of classes and add one so most of the code must be rewritten at runtime with emit api anyway.

@KalleOlaviNiemitalo
Copy link
Contributor

Related: dotnet/csharplang#8942, but that talks about private constructors used by nested classes

@KalleOlaviNiemitalo
Copy link
Contributor

Also dotnet/csharplang#8927, which proposes marking closed class hierarchies with a dedicated keyword in the source code, rather than than inferring that from the accessibility of constructors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

3 participants