-
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: Negative Pattern #246
Comments
Really? How would that work for recursive patterns? |
Perhaps this would be only useful as a top-level pattern in |
In that case this seems like an awful lot of language baggage for something you can already express with |
!is or is! |
I personally feel like having |
@ivankarpey I think you're confusing expressions and patterns. The proposal is that if (e is !true) // if e is false
if (e is not true) // if e is anything other than true |
@gafter that make sense for a pattern matching cases with types for instance. But for bool value there |
The operand doesn't have to be object o = GetSomeRandomObject();
if (o is !true) {
DoSomething();
} And that means: if (o is bool && (bool)o == false) {
DoSomething();
} But that would inherently behave differently to the if (o is not true) {
DoSomething();
} would mean: if (!(o is bool && (bool)o)) {
DoSomething();
} That's why new syntax is necessary. |
@HaloFour the fact that |
But it's legal and has been since C# 1.0, so it's not going anywhere.
Which is fine, but This proposal isn't about adding keyword versions of existing C# operators. It's about creating additional patterns, including one that can negate. If it weren't for the fact that |
static void M(bool? b)
{
if (b is true)
{
// b == true;
}
if (b is !true)
{
// b == !true
// i.e. b == false
}
if (b is not true)
{
// b != true
// i.e. b == false || b == null
}
} |
|
@HaloFour in this case, |
@HaloFour I understand the reasoning for adding the "not" keyword, but if "!is" became legal, then the keyword would no longer be needed, the syntax would be consistent with all other uses of the "!" symbol, and whitespace around it would not have any meaning. It may not look pretty, but this is a "C" based language, so it doesn't need to be. This is not Visual Basic; therefore, it doesn't need to read like "English", nor should it. |
Patterns can be applied in other places, though, not just with |
Closing in favor of championed issue #1350 |
Copied from: dotnet/roslyn#16766
Negative Pattern
I propose to add a pattern operator to negate the effect of another pattern,
A "negative pattern" could be applied to an arbitrary pattern and reverse the failure path.
Examples:
It could be even nested in recursive patterns as well,
This would be consistent with other proposed pattern operators like #118.
Definitive assignment rules are reversed, for example,
This also eliminates the need to parenthesize the
is
expression in case you want to negate the result.Note that negating a simple type check requires a discard since
is
and recursive patterns have different binding precedence.The text was updated successfully, but these errors were encountered: