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

Switch expressions cannot infer shared type #35443

Closed
dkmiller opened this issue May 2, 2019 · 2 comments
Closed

Switch expressions cannot infer shared type #35443

dkmiller opened this issue May 2, 2019 · 2 comments

Comments

@dkmiller
Copy link

dkmiller commented May 2, 2019

Version Used:
Visual Studio Version 16.0.3 + dotnet core 3.0.100-preview3-010431

Steps to Reproduce:

object Foo(string x)
{
    return x switch
    {
        "a" => 1,
        "b" => "hi",
        _ => throw null
    };
}

Expected Behavior:

Given the signature of Foo, the compiler should be able to infer that the return type of the switch expression is object.

Actual Behavior:

The compiler returns error

CS8506 C# No best type was found for the switch expression.

If I add a cast to object in any of the cases in the switch statement (e.g. "a" => (object)1) then the code compiles.

@svick
Copy link
Contributor

svick commented May 3, 2019

I believe this is the expected behavior for the current preview version of C# 8.0. But it will almost certainly behave the way you suggested in the final version of C# 8.0, see dotnet/csharplang#2389.

@dkmiller
Copy link
Author

dkmiller commented May 3, 2019

@svick that's great to know, and very exciting news! I think this issue can safely be closed then.

@dkmiller dkmiller closed this as completed May 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants