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

Default interface implementation of static abstract operator is not resolved. #83007

Open
gurustron opened this issue Mar 5, 2023 · 1 comment

Comments

@gurustron
Copy link

gurustron commented Mar 5, 2023

Description

Not sure if this a bug though and maybe this is better addressed in the csharplang repo.

When interface provides implementation for IEqualityOperators.== operator the implementation is not resolved unless via generic indirection.

Original SO question

Reproduction Steps

public interface IAutoEquatable<T> : IEquatable<T>, IEqualityOperators<T, T, bool> where T : IAutoEquatable<T>
{
    static bool IEqualityOperators<T, T, bool>.operator ==(T? left, T? right)
    {
        Console.Write("ieop.== ");
        if (ReferenceEquals(left, null) && ReferenceEquals(right, null))
        {
            return true;
        }

        if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
        {
            return false;
        }

        return left.Equals(right);
    }

    static bool IEqualityOperators<T, T, bool>.operator !=(T? left, T? right)
    {
        return !(left == right);
    }
}

class Foo : IAutoEquatable<Foo>
{
    public bool Equals(Foo? other)
    {
        Console.Write("eq ");
        return true;
    }
}

And usage:

// 1
Do<Foo>();
// 2
Do1<Foo>();
// 3
Console.WriteLine(new Foo() == new Foo());
// 4
IEqualityOperators<Foo, Foo, bool> foo1 = new Foo(); 
IEqualityOperators<Foo, Foo, bool> foo2 = new Foo();
Console.WriteLine(foo1 == foo2);
// 5
IAutoEquatable<Foo> foo3 = new Foo();
IAutoEquatable<Foo> foo4 = new Foo();
Console.WriteLine(foo3 == foo4);

void Do<T>() where T :IAutoEquatable<T> , new()
{
    Console.WriteLine(new T() == new T());
}

void Do1<T>() where T : IEqualityOperators<T, T, bool> , new()
{
    Console.WriteLine(new T() == new T());
}

Expected behavior

  1. ieop.== eq True
  2. ieop.== eq True
  3. ideally would be great to get ieop.== eq True but based on the fact that classes do not inherit members from its interfaces (default interface implementation spec) False can be somewhat explained
  4. ieop.== eq True
  5. ieop.== eq True

Actual behavior

  1. ieop.== eq True
  2. ieop.== eq True
  3. False
  4. False
  5. False

Regression?

NA

Known Workarounds

NA

Configuration

  • OS
    Windows 11
  • SDK
    7.0.100 [C:\Program Files\dotnet\sdk]

Other information

@sharplab

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 5, 2023
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@gurustron gurustron changed the title Default interface implemented static abstract operators do not resolve. Default interface implementation of static abstract operator is not resolved. Mar 5, 2023
@mangod9 mangod9 removed the untriaged New issue has not been triaged by the area owner label Mar 22, 2023
@mangod9 mangod9 added this to the Future milestone Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants