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

Merge inherited interface methods according to variance #102121

Open
alrz opened this issue May 12, 2024 · 3 comments
Open

Merge inherited interface methods according to variance #102121

alrz opened this issue May 12, 2024 · 3 comments
Labels
area-TypeSystem-coreclr untriaged New issue has not been triaged by the area owner

Comments

@alrz
Copy link
Member

alrz commented May 12, 2024

In the following code public void M(object arg) should be enough to implement the interface but neither the compiler nor the runtime accept such code and an explicit impl is required to bridge the two overloads. (sharplab.io)

interface ISomeInterface<in T> {
  void M(T arg);
}

interface IBase : ISomeInterface<object>;

interface IDerived : IBase, ISomeInterface<string> {
   // uncomment to resolve the error but this shouldn't be necessary
   // void ISomeInterface<string>.M(string obj) => M(obj as object); // calls base
}

class C : IDerived {
  // error: method `M(string)` is not implemented
  public void M(object arg) { }
}
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 12, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 12, 2024
@CyrusNajmabadi
Copy link
Member

i thnik this would take a language change. i don't think the variance part of the lang spec supports this. The variance part affects things like conversions, but not interface impls.

@alrz
Copy link
Member Author

alrz commented May 12, 2024

Indeed, but since identical method signatures from different interfaces are already merged, I think this would be just a relaxation of that rule in both sides. On the runtime, the variance already guaranteed that the call is safe.

@CyrusNajmabadi
Copy link
Member

I'm not opposed to this being allowed. But would need runtime to push for wanting this. Otherwise, it's unlikely we would do it on the lang side.

@vcsjones vcsjones added area-TypeSystem-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TypeSystem-coreclr untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants