Behavior difference between "a is not X" and "!(a is X)" patterns
See original GitHub issueVersion Used: Visual Studio 2022 v17.2.4
Steps to Reproduce:
- Have a
!(variable is ClassName)
pattern in a context whereClassName
is also a property name. - Invoke the CodeFix for
IDE0083
to convert this to anis not
pattern. - Compilation fails after this.
See sharplab snippet:
using System;
using System.Linq.Expressions;
public class C {
private string LambdaExpression { get; set; }
public void M() {
Expression expression = null;
if (!(expression is LambdaExpression))
Console.WriteLine("1: not a lambda expression");
// error CS0029: Cannot implicitly convert type 'string' to 'System.Linq.Expressions.Expression'
// this one picks up the string property, rather than the type
//if (expression is not LambdaExpression)
// Console.WriteLine("2a: not a lambda expression");
if (expression is not LambdaExpression _)
Console.WriteLine("2b: not a lambda expression");
if (expression is not System.Linq.Expressions.LambdaExpression)
Console.WriteLine("3: not a lambda expression");
}
}
Expected Behavior: Either:
- Code still compiles afterwards, or
- The CodeFix for
IDE0083
does not convert the expression.
Actual Behavior: Code fails to compile after invoking the CodeFix, because it attempts to match against the property name (or its value?) I’m not sure what it is even trying to do there, because it can’t possibly be parsed as constant pattern; and it doesn’t match as a type pattern (which would be the expected thing). It does work if I add a variable name or discard at the end (which turns it into a declaration pattern).
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Understanding the 4 Personality Types: A, B, C, and D
There are four basic personality types: A, B, C, and D. Each has strengths and weaknesses influencing hiring and retention, as well as...
Read more >Exploring Behavioral Patterns for Data-Driven Modeling of ...
However, few attempts have been done to connect and compare behavioral patterns with known dimensions of individual differences.
Read more >Chaos theory
Chaos theory is an interdisciplinary area of scientific study and branch of mathematics focused on underlying patterns and deterministic laws of ... Although...
Read more >Initial Condition and Behavior Patterns in Learning Dynamics
The close relationships between behavior patterns and ideas and attitudes are ... To construct the proportions X = IND/PER, Y = POS/NEG, ...
Read more >Effect of pattern awareness on the behavioral and ...
A common assumption is that statistical learning is a type of implicit learning that does not result in explicit awareness of learned patterns....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is expected from the language/compiler side and results from backwards compatibility concerns with
is
expressions which predate patterns.From the patterns doc: “Pattern matching introduces a new context where the expression-type ambiguity arises. Previously the right-hand-side of an
is
operator was a type. Now it can be a type or expression, and if it is a type it may be followed by an identifier.”In other words, an is-expression previously would only bind to a type (and this has to be maintained for compatibility reasons), but an is-pattern can bind to a type or a (constant) expression.
I’m moving this issue to the IDE. Let’s see if we can avoid offering the fix in this situation.
Oh, sorry, I misunderstood that.
Yes, I just tried this on a test project, I get the refactoring “Use pattern matching” für
IDE0083
in both cases (property in a base class, property in the same class). Both cause compilation to fail.