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

Strange behaviour of pattern matching #39721

Closed
vabka opened this issue Nov 7, 2019 · 2 comments
Closed

Strange behaviour of pattern matching #39721

vabka opened this issue Nov 7, 2019 · 2 comments
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design

Comments

@vabka
Copy link

vabka commented Nov 7, 2019

Version Used: 3.1.100-preview1-014459 (but reproduced in earlier versions)

Steps to Reproduce:

using System;
static class Demo 
{
    static void NotOkCase(object obj)
    {
        if((obj is string s) == false) 
        {
             Console.WriteLine("obj is not a string");                
        }
        else
        {
            //uh-oh use of unassigned variable
            //Console.WriteLine(s);
        }
    }
    
    static void OkCase(object obj)
    {
        if(!(obj is string s)) 
        {
            Console.WriteLine("obj is not a string");
        }
        else 
        {
            Console.WriteLine(s);
        }
    }
}

Expected Behavior:
I can use 's' in NotOkCase.

Actual Behavior:
I can't.

@vabka vabka changed the title Strange behaviour of pattern matching (obj is Type val) Strange behaviour of pattern matching Nov 7, 2019
@CyrusNajmabadi
Copy link
Member

This is how the language works. There is no understanding about how == bool-val works wrt to definite-assignment.

If you want this to work, you'll have to file a language request over at csharplang.

@gafter
Copy link
Member

gafter commented Nov 7, 2019

You need to use ! to negate the expression, which is specified to preserve definite assignment states. == false does not do the same.

@gafter gafter closed this as completed Nov 7, 2019
@gafter gafter added Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design labels Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

3 participants