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

Overload resolution should not consider unreferenced types #3561

Closed
TonyValenti opened this issue Jun 11, 2020 · 3 comments
Closed

Overload resolution should not consider unreferenced types #3561

TonyValenti opened this issue Jun 11, 2020 · 3 comments

Comments

@TonyValenti
Copy link

Let's say I have the following Methods:

public class MyClass {
  public void Foo(string x){ }
  public void Foo(int x){ }
  public void Foo(SomeCrazyTypeFromAThirdPartyAssembly x){ }
}

If I want to call Foo("asdf"), the calling class has to have a reference to SomeCrazyTypeFromAThirdPartyAssembly even though I have no intention of using it. Can this restriction be removed? It should only consider types that the calling assembly references.

@333fred
Copy link
Member

333fred commented Jun 11, 2020

No, this restriction cannot be removed. At a fundamental level we have to know what all the types involved mean, because that other type could actually cause an ambiguity or it might end up being a better match. For example:

Assembly 1:

class A { }

Assembly 2 (references 1):

class B
{
    public static implicit operator B(A a) => null;
}

Assembly 3 (references 1):

class C
{
    public static implicit operator C(A a) => null;
}

Assembly 4 (references 1, 2, 3):

class D
{
    public void M1(B b, int i = 1) { }
    public void M1(C c) { }
}

Assembly 5 (references 1, 2, 4):

public class E
{
    public void M2(A a) => D.M1(a);
}

If we allowed Assembly 5 to compile, then overload resolution would erroneously pick M(B b, int i = 1), even though M1(C c) is a better match.

@333fred 333fred closed this as completed Jun 11, 2020
@TonyValenti
Copy link
Author

Except it is definitely not a better Match if the necessary assemblies aren’t in scope.

@333fred
Copy link
Member

333fred commented Jun 12, 2020

No, it's undefined whether there's a better match. You can't just part of a class's definition that you don't like.

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