Skip to content
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

Support structurally closed hierarchies like Discriminated Unions #42

Open
csharper2010 opened this issue Apr 4, 2022 · 5 comments
Open
Labels
enhancement New feature or request

Comments

@csharper2010
Copy link
Contributor

It would be helpful to detect the closed hierarchy of a class with private constructor and nested subclasses having either also a private constructor or a sealed modifier. This is a way to simulate the desparately missing discriminated unions safely in C#.

public abstract class Result<TSuccess, TError> {
    private Result() { }

    public sealed class Success : Result<TSuccess, TError> {
        public TSuccess Value { get; }
        public Success(TSuccess value) { Value = value; }
    }

    public sealed class Error : Result<TSuccess, TError> {
        public TError Value { get; }
        public Error(TError value) { Value = value; }
    }
}

A switch expression like this should provide the error:

var x = result switch
{
    Result<string, string>.Error error => ""Error: "" + error,
    _ => throw ExhaustiveMatch.Failed(result),
};

This would be o.k.:

var x = result ◊1switch{
    Result<string, string>.Error error => ""Error: "" + error,
    Result<string, string>.Success success => ""Success: "" + success,
    _ => throw ExhaustiveMatch.Failed(result),
}
csharper2010 added a commit to csharper2010/ExhaustiveMatching that referenced this issue Apr 4, 2022
…lkerCodeRanger#42)

Detecting closed hierarchies like discriminated unions without the need to
provide [Closed]-Attribute
@WalkerCodeRanger
Copy link
Owner

A similar case applies even when the subclasses are not nested classes, but all the constructors of the base class are private or private protected.

@WalkerCodeRanger WalkerCodeRanger added the enhancement New feature or request label May 16, 2022
@csharper2010
Copy link
Contributor Author

Yes, i did not consider private protected... could add that in another step.

So if there is at least one private protected constructor and all constructors are private or private protected and all subclasses (recursively) in the compilation unit are either sealed or also have at most private protected visibility in constructor, the hierarchy is also known to be structurally closed.

WalkerCodeRanger added a commit that referenced this issue Jun 25, 2022
Implement support for structurally detectable closed hierarchcies (#42)
@csharper2010
Copy link
Contributor Author

@WalkerCodeRanger
Are there chances to get a current build soon published to nuget?
Do you have open things to do where I could support?

@chtenb
Copy link

chtenb commented Jun 12, 2023

Is this project still maintained?

@chtenb
Copy link

chtenb commented Jun 13, 2023

@csharper2010 Have you published your version on nuget by any chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants