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

Use for comparing by interface? #40

Open
rwv37 opened this issue Mar 13, 2024 · 0 comments
Open

Use for comparing by interface? #40

rwv37 opened this issue Mar 13, 2024 · 0 comments

Comments

@rwv37
Copy link

rwv37 commented Mar 13, 2024

I just tried using this library on a little stub of a class:

    internal sealed class PortCategory(string name) : ComparableBase<IPortCategory>, IPortCategory
    {
        static PortCategory()
        {
            DefaultComparer = ComparerBuilder.For<IPortCategory>()
                .OrderBy(x => x.Name);
        }

        public string Name { get; } = name ?? throw new ArgumentNullException(nameof(name));
    }

That fails compilation, with:

The type 'Rwv37.FreeBSD.VestertopianPortsBuilder.Core.Interfaces.IPortCategory' cannot be used as type parameter 'T' in the generic type or method 'ComparableBase<T>'. There is no implicit reference conversion from 'Rwv37.FreeBSD.VestertopianPortsBuilder.Core.Interfaces.IPortCategory' to 'Nito.Comparers.ComparableBase<Rwv37.FreeBSD.VestertopianPortsBuilder.Core.Interfaces.IPortCategory>'.

I then noticed that the sample in the docs implements ComparableBase of the class itself, and after looking at the source code it, it looks to me that this is required (?).

If I change ComparableBase<IPortCategory> to ComparableBase<PortCategory>, the compiler error goes away, but it's replaced by others, because PortCategory no longer implements IPortCategory (due to IPortCategory being both IComparable<IPortCategory> and IEquatable<IPortCategory>).

Of course in the case of this little stub class, it might be reasonable to just get rid of IPortCategory entirely, but that doesn't seem like it's necessarily a good solution in other, presumably more complex, cases.

Is there a way to use this library to do what I was hoping to do here? Thanks in advance.

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

1 participant