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

Type inference bug? #18770

Closed
sunnycase opened this issue Apr 18, 2017 · 2 comments
Closed

Type inference bug? #18770

sunnycase opened this issue Apr 18, 2017 · 2 comments
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design

Comments

@sunnycase
Copy link

Version Used: Visual Studio 2017

Steps to Reproduce:

    public static class NullExt
    {
        public static T? Some<T>(this T? value, Action<T> func) where T : struct
        {
            if (value != null)
                func(value.Value);
            return value;
        }

        public static T Some<T>(this T value, Action<T> func) where T : class
        {
            if (value != null)
                func(value);
            return value;
        }
    }

    class Program
    {
        public static void Main()
        {
            string s = "s";
            s.Some(_ => Console.Write("some"));

            int? i = 1;
            i.Some(_ => Console.Write("some"));
        }
    }

Expected Behavior:
This code snippet compiles.

Actual Behavior:
This code snippet doesn't compiles.
error CS0121: The call is ambiguous between the following methods or properties: “NullExt.Some(T?, Action)” and “NullExt.Some(T, Action)”

Change

            i.Some(_ => Console.Write("some"));

to

            i.Some<int>(_ => Console.Write("some"));

it compiles, So i think it's a type inference bug. What do you think of this?

@shikimoon
Copy link

It's very helpful to me

@gafter
Copy link
Member

gafter commented Apr 18, 2017

This is probably not a bug, but a case where dotnet/csharplang#98 would help.

@gafter gafter added the Resolution-By Design The behavior reported in the issue matches the current design label Apr 19, 2017
@gafter gafter closed this as completed Apr 19, 2017
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

4 participants