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

[C# Feature] Local Enums #16233

Closed
RandyBuchholz opened this issue Jan 4, 2017 · 2 comments
Closed

[C# Feature] Local Enums #16233

RandyBuchholz opened this issue Jan 4, 2017 · 2 comments
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue

Comments

@RandyBuchholz
Copy link

Background

Local Functions are a great new feature. One of the uses I've see for local functions is local decision making. For example, functions containing switch statements. Switching on Enum values is common.

A side effect of local function capability is that it creates additional layers of scope. A best-practice is to declare variables near the point of use, for clarity. When we had only one level of scope inside a function, declaring enums at the class level was "close enough". While the idea of declaring local enums has come up before, I think they were more nice to have's than anything else. I believe that the introduction of local functions makes this something to reconsider.

Proposed

Allow declaring enumerations inside methods.

Justification

Using an enumeration inside a local function now requires "declaring a 'variable'" at least two levels above the point of use.

public class MyClass {
    public enum SwitchEnum { Option1, Option2 }
    // do stuff
    // .... stuff that doesn't care about SwitchEnum
    
    // Method that has no concept of SwitchEnum
    public string SomeMethod(){ }

    // Method the "almost" cares about SwitchEnum
    public int OuterMethod {
        // Do stuff
        // Method that cares about SwitchEnum
        int DecisionMethod( SwitchEnum switchValue ){
            switch (switchValue) {
                case switchValue.Option1: { return 38; }
            }
        }
    }
}

If there was ever any valid reason to put a local in a local, the enum gets even farther removed.

Clearer is

public class MyClass {
    public int OuterMethod {
        public enum SwitchEnum { Option1, Option2 }
        // Setup for decision
        var switchValue = SwitchEnum.Option2; 
        int DecisionMethod( SwitchEnum switchValue ){
            switch (switchValue) {
                case switchValue.Option1: { return 38; }
            }
        }
    }
}

The scope of declaration should mean that the declared item has meaning within that scope. Currently, this will not always be the case with local functions and enums.

I think this case makes it worth revisiting the idea of local enums.

@alrz
Copy link
Contributor

alrz commented Jan 4, 2017

If switchValue is always a constant why not use a constant instead? Also, in case of multiple calls to the local function with various constants, wouldn't it make sense to define various local functions in the first place?

@gafter
Copy link
Member

gafter commented Jan 4, 2017

This is a dup of #9523

@gafter gafter closed this as completed Jan 4, 2017
@gafter gafter added Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue labels Jan 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

3 participants