question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Behavior difference between "a is not X" and "!(a is X)" patterns

See original GitHub issue

Version Used: Visual Studio 2022 v17.2.4

Steps to Reproduce:

  1. Have a !(variable is ClassName) pattern in a context where ClassName is also a property name.
  2. Invoke the CodeFix for IDE0083 to convert this to an is not pattern.
  3. 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:

  1. Code still compiles afterwards, or
  2. 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:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jcouvcommented, Jun 22, 2022

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.

0reactions
BhaaLseNcommented, Jun 24, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found