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

[Proposal] Allowance of Enumerate Local Methods #3320

Closed
HTGAzureX1212 opened this issue Mar 29, 2020 · 6 comments
Closed

[Proposal] Allowance of Enumerate Local Methods #3320

HTGAzureX1212 opened this issue Mar 29, 2020 · 6 comments

Comments

@HTGAzureX1212
Copy link

Summary

In some other programming languages, like Java, it is permitted to have local methods in enumerates, for example:

public enum Enumerate
{
    Value1, Value2, Value3;

    private Enumerate()
    {
        // More code here
    }
}

Motivation

Like I said above, in Java, it is possible. Though I have used C# for quite a while, I would like to propose this feature. The current way to do so, in C#, is a little bit different, with extension methods:

using System;

enum Something
{
    Thing1, 
    Thing2
}

class Program
{
    public static string GetString(this Something something)
    {
        switch(something)
        {
            case Something.Thing1:
                return "Thing1";
            case Something.Thing2:
                return "Thing2";
            default:
                return "Default";
        }
    }
    
    static void Main(string[] args)
    {
        Something something = Something.Thing1;
        string str = something.GetString();
        Console.WriteLine(str);
    }
}

As you can see, it is much longer and more complicated.

Detailed Design

Syntax

The syntax, proposed for this new feature, is as follows:

  • Inside an enumerate, the accessibility level private is ONLY accepted for its instantiation;
  • Other than that, all the other things are the same; and
  • A semi colon is expected after all its values are created.

So now, according to our syntax proposal, the proposed syntax will be like this:

enum Something
{
    Thing1,
    Thing2;

    private Something()
    {
        
    }

    public void MyMethod()
    {

    }
}
@YairHalberstadt
Copy link
Contributor

There's plans to introduce Discriminated unions to C# next release.
There's a draft level proposal here: https://github.com/dotnet/csharplang/blob/master/proposals/discriminated-unions.md

Based on the linked proposal, I would guess the following would be legal:

public partial class enum Enumerate
{
    Value1, Value2, Value3;
}

public partial class Enumerate
{
    private Enumerate()
    {
        // More code here
    }
}

Whilst the partial is unfortunate, remember this proposal has not been fully fleshed out yet. By the time it's done I imagine you would be able to simply write:

public class enum Enumerate
{
    Value1, Value2, Value3;

    private Enumerate()
    {
        // More code here
    }
}

@HTGAzureX1212
Copy link
Author

Yeah thanks

@HTGAzureX1212
Copy link
Author

Would also suggest to have variables inside the enum:

public enum Enumerate
{
    // some values go here

    public string str = "something";
}

@YairHalberstadt
Copy link
Contributor

I think my response would be identical - based on the current proposal you would have to do that using partial, but I would hope that restriction is removed by the time this is released.

@yaakov-h
Copy link
Member

Duplicate of #297?

@333fred
Copy link
Member

333fred commented Mar 29, 2020

Closing as duplicate.

@333fred 333fred closed this as completed Mar 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants