-
Notifications
You must be signed in to change notification settings - Fork 1k
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: Overlapping pattern variable names in switch #118
Comments
In the top example, if there's a chance that This would be consistent with intersection types where if |
Would using |
Not sure but supposedly if an intersection of A and B is implicitly inherited from C it probably wouldn't be a breaking change. |
I don't believe the use of
If you ever end up in a case where
|
I'm relying on #33 for the definition of "most specific common type". So the answer depends on the rules outlined in that proposal. Currently it doesn't consider interfaces at all.
If intersections defined such that "as long as both types inherited from X, the resultant intersection type will be implicitly inherited from X", that would work. However, your example still requires #33 to also consider interfaces to begin with. |
The proposal is interesting mostly in scenarios where one wants to test for some, but not all of the derived types. Say
On the other hand, when testing only for I suggest adding another derived type to your example above to highlight this aspect. |
So this is basically a shorter and slightly more efficient way to write |
@svick Consider the example from spec draft: This would avoid the code duplicated in some case bodies, Expr Simplify(Expr e)
{
switch (e) {
case Mult(Const(1), var x):
case Mult(var x, Const(1)):
return Simplify(x);
// ...
case Add(Const(0), var x):
case Add(var x, Const(0)):
return Simplify(x);
}
} |
That example doesn't need the "most specific common type" though, "identical types" would be sufficient. |
What is "identical types" by the way? |
It's a special case of a common type where |
Closing in favor of championed issue #1350 |
Overlapping pattern variable names in switch
Summary
Relax pattern-matching in switch labels to allow overlapping variable names.
Motivation
When we are matching a value against multiple patterns, sometimes we do not care about the exact matching pattern, just that it's matched with either of patterns. Currently this produces an error.
This proposal aims to make that possible.
Proposal
The real issue is that what is the type of
x
? As the resulting value will be of either typesA
orB
, we can infer the most specific common type forx
i.e.C
.So the above code will be roughly equivalent to the following:
Note: Currently
??
operator does not infer the most specific common type so we will need to upcast the result of either side.Alternatives
Ported from dotnet/roslyn#6235
While this does not need any new syntax, yet it cannot be nested in recursive patterns. As an alternative we can introduce OR-patterns which is also usable in
is
expressions.Variations
In dotnet/roslyn#6235, an AND pattern is also proposed that matches a value against multiple patterns and succeeds only if none of patterns fail to match.
Drawbacks
Since that would be an operator, we will need to specify a precedence and possibly introduce "parenthesized patterns" which will add further complexities to the pattern syntax.
Note: this would not be applied to the main proposal as it is merely a relaxation of existing syntax.
The text was updated successfully, but these errors were encountered: