You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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?
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.
If there was ever any valid reason to put a local in a local, the enum gets even farther removed.
Clearer is
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.
The text was updated successfully, but these errors were encountered: