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

Better Betterness is not that better #8942

Closed
marek-safar opened this issue Feb 19, 2016 · 6 comments
Closed

Better Betterness is not that better #8942

marek-safar opened this issue Feb 19, 2016 · 6 comments
Labels
Area-Compilers Area-Language Design Bug Tenet-Compatibility Violation of forwards/backwards compatibility in a design-time piece.
Milestone

Comments

@marek-safar
Copy link
Contributor

marek-safar commented Feb 19, 2016

Consider following example

using System;

public class BrokenOverloadResolution
{
    static void Test (Action a)
    {
    }

    static void Test (Func<object> f)
    {
    }

    public void MethodGroupExactMatching ()
    {
        // This fails to compile with
        // 
        // error CS0121: The call is ambiguous between the following methods or properties: 'BrokenOverloadResolution.Test(Action)' and 'BrokenOverloadResolution.Test(Func<object>)'
        //
        Test (Listen);

        //
        // non-void version works
        //
        // Test2 (DontListen);
    }

    void Listen ()
    {
    }

    object DontListen ()
    {
        return null;
    }

    public static void Main ()
    {
    }
}

Compilation produces after #6560 fix error message CS0121: The call is ambiguous between the following methods or properties: 'BrokenOverloadResolution.Test(Action)' and 'BrokenOverloadResolution.Test(Func<object>)' but there is no ambiguity.

Removing "ambiguous" candidate static void Test (Action a) leads to real compilation error which says error CS0407: 'void BrokenOverloadResolution.Listen()' has the wrong return type which makes sense as void is not convertible to object but that means there is no real ambiguity.

Reading #7934 does not mentioned anything special about method group conversions with return type void.

@Joe4evr
Copy link

Joe4evr commented Feb 19, 2016

Isn't this what #250 (bestest betterness) is supposed to fix?

@gafter gafter added Area-Language Design Tenet-Compatibility Violation of forwards/backwards compatibility in a design-time piece. Bug labels Feb 20, 2016
@gafter gafter added this to the 1.3 milestone Feb 26, 2016
@jaredpar jaredpar modified the milestones: 1.3, 2.0 (RC) Mar 31, 2016
@gafter gafter modified the milestones: 3.0, 2.0 (RC) Jul 19, 2016
@markhurd
Copy link

markhurd commented Aug 2, 2016

Just noting the equivalent VB.NET code has worked since at least VS2013.

@jaredpar
Copy link
Member

jaredpar commented May 5, 2017

Chatted with @gafter and this is indeed an example of the bestest betterness discussion. In particular point 3:

  • For a method group conversion, candidate methods whose return type doesn't match up with the delegate's return type are removed from the set.

Going to let the language design issue track this for now. It's on our candidate list for an upcoming version

dotnet/csharplang#98

@jaredpar jaredpar closed this as completed May 5, 2017
@Clancey
Copy link

Clancey commented May 5, 2017

Additionally, it has issues with Linq.

var files = new string[0];
var maxDate = files.Max(System.IO.File.GetCreationTime);

Error CS0121: The call is ambiguous between the following methods or properties: 'Enumerable.Max(IEnumerable, Func<TSource, int>)' and 'Enumerable.Max(IEnumerable, Func<TSource, int?>)'

However slightly changing the code works:

files.Max(x=> System.IO.File.GetCreationTime(x));

@gafter
Copy link
Member

gafter commented May 6, 2017

@jaredpar Actually, now that I think about it further it might not be the same. This is a lambda conversion, not a method group conversion. I'll note to look at that too when we look at bestest betterness.

@jaredpar
Copy link
Member

jaredpar commented May 6, 2017

@gafter the most recent comment is a lambda conversion. The original question is about method group conversions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Area-Language Design Bug Tenet-Compatibility Violation of forwards/backwards compatibility in a design-time piece.
Projects
None yet
Development

No branches or pull requests

6 participants