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

Allow type pattern matching to have multiple expression bind to the same variable #1980

Closed
lostmsu opened this issue Nov 3, 2018 · 9 comments

Comments

@lostmsu
Copy link

lostmsu commented Nov 3, 2018

Excerpt from own Roslyn-based code:

switch(...) {
...
case var _ when defaultValue is LambdaExpressionSyntax lambda:
case var _ when defaultValue is ParenthesizedExpressionSyntax parenthesized
        && (lambda = parenthesized.Expression as LambdaExpressionSyntax) != null:
  ...
  break;
}

Should be able to do:

switch(...) {
...
case var _ when defaultValue is LambdaExpressionSyntax lambda:
case var _ when defaultValue is ParenthesizedExpressionSyntax parenthesized
         && parenthesized.Expression is LambdaExpressionSyntax lambda:
  ...
  break;
}

Basically, in expr is Type variable why can't variable be any existing variable of a matching type (e.g. assignable from Type). I'd even be ok with expr is Type out variable, as long as I don't have to do the (... = ... as ...) != null

@alrz
Copy link
Contributor

alrz commented Nov 3, 2018

Relates to #118, #1350

@lostmsu
Copy link
Author

lostmsu commented Nov 3, 2018

Now I realize this should not be limited to switch statements. It should be possible to do it in any expression/statement.
E.g. success = other is XXX xxx || other is YYY yyy && yyy.Val is XXX xxx;

@HaloFour
Copy link
Contributor

HaloFour commented Nov 3, 2018

Now I realize this should not be limited to switch statements.

That's where the or pattern potentially comes into play.

@lostmsu
Copy link
Author

lostmsu commented Nov 3, 2018

That's where the or pattern potentially comes into play.

Why is there a need for a new syntax? The above example is just a simple logical expression.

@HaloFour
Copy link
Contributor

HaloFour commented Nov 3, 2018

@lostmsu

Because the use of || with patterns already means something else. This is discussed in more detail in the issues that @alrz posted.

@lostmsu
Copy link
Author

lostmsu commented Nov 3, 2018

@HaloFour my understanding is that or in above is a special syntax to replace case 1: case 2: with case 1 or 2:, and does not have anything to do with type testing pattern matching.

@HaloFour
Copy link
Contributor

HaloFour commented Nov 3, 2018

@lostmsu

and does not have anything to do with type testing pattern matching.

Those "type tests" are just a very limited form of pattern, just like those constant patterns. C# 8.0 will greatly expand on the types of supported patterns, including recursive patterns, and or will be a way to combine patterns. Rewriting your example with recursive property patterns and or patterns might look like this:

success = other is XXX xxx or YYY { Val: XXX xxx };

Assuming that or patterns allow you to declare the same variable on both sides as long as they're compatible.

@HaloFour
Copy link
Contributor

HaloFour commented Nov 3, 2018

That said, if you're looking for support for this outside of pattern matching, such as with case guards or Boolean expressions, that would be its own proposal.

@YairHalberstadt
Copy link
Contributor

Closing as championed in #4018

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