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

Proposal: Negative Pattern #16766

Closed
alrz opened this issue Jan 26, 2017 · 4 comments
Closed

Proposal: Negative Pattern #16766

alrz opened this issue Jan 26, 2017 · 4 comments

Comments

@alrz
Copy link
Contributor

alrz commented Jan 26, 2017

Moved from: #16742 (comment)

I propose to add a pattern operator to negate the effect of another pattern,

not-pattern:
not pattern

A "negative pattern" could be applied to an arbitrary pattern and reverse the failure path.

Examples:

if (obj is not null)

switch (obj) {
  case not Constant:
      break;
}

It could be even nested in recursive patterns as well,

case (not 0, var y):
// equivalent to
case (var x, var y) when x != 0:

This would be consistent with other proposed pattern operators like #6235.

Note that definitive assignment rules are reversed, for example,

if (obj is not T t) return;
// t is definitely assigned

This also eliminates the need to parenthesize the is expression in case you want to negate the result.

@gordanr
Copy link

gordanr commented Jan 26, 2017

Very interesting. Can you give more examples, please.

@alrz
Copy link
Contributor Author

alrz commented Jan 26, 2017

To clarify, this is just another "recursive pattern", once those are introduced to the language you can imagine more usages from combining various kinds of patterns.

@Thaina
Copy link

Thaina commented Jan 27, 2017

is! and case! should just be valid syntax

Recursive pattern should be just as it is

@alrz
Copy link
Contributor Author

alrz commented Mar 21, 2017

Moved to dotnet/csharplang#246.

@alrz alrz closed this as completed Mar 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants