Annoying scope of pattern matching variables #8776
Replies: 3 comments 9 replies
-
This behavior is quite intentional and the result of a lot of discussions regarding the scenarios supported by different scoping rules for pattern variables. There are probably numerous meeting notes on the subject, but I believe this is where "leaky scope" was introduced: https://github.com/dotnet/csharplang/blob/main/meetings/2016/LDM-2016-07-15.md I believe there was some degree of interest in perhaps permitting shadowing the variables in those scopes where the existing variable was not definitely assigned, or maybe exploring "flow scoping" (which is what you described, and how Java approached pattern variables), but I don't believe either conversation has gone anywhere. |
Beta Was this translation helpful? Give feedback.
-
Related to #8622, though more liberal than that proposal allows. |
Beta Was this translation helpful? Give feedback.
-
Actually, there is another alternative, you can just introduce nested scopes (although this may also be annoying):
If this is implemented then it would actually prevent #8622 completely, because that proposal is all about "reusing a pattern variable in a different branch". |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Do we currently have an active issue that tracks improving the scope of pattern matching variables?
Like for cases like this.
This seems quite silly especially given pattern matching variable can't be used unless the expression definitively return true anyway
To say it simply, pattern matching variable scope should be equal to where that variable is definitively assigned.
the alternative to the above would have been naming the variables obj, obj2 and obj3 which is annoying
Beta Was this translation helpful? Give feedback.
All reactions