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

[Feature] specify EqualityComparer #119

Open
ndrwrbgs opened this issue Sep 29, 2019 · 0 comments
Open

[Feature] specify EqualityComparer #119

ndrwrbgs opened this issue Sep 29, 2019 · 0 comments

Comments

@ndrwrbgs
Copy link
Contributor

Preamble

  • Fody 6.0.0
  • Equals.Fody 4.0.0
  • MSBuild 16

Describe the issue

When comparing things like string, often you need to specify the IEqualityComparer to use for the comparison. I'd like to see(/add, it's important enough to me that Equality not be so much boilerplate in projects) the ability to specify the EqualityComparer for a field/property to be used by the generated code.

Minimal Repro/Sample

There are a couple different possibilities given in the below.

[Equals]
class TypeUnderTest : IEquatable<TypeUnderTest>
{
  private static readonly IEqualityComparer<String> NameComparer = StringComparer.OrdinalIgnoreCase;
  
  public int Number { get; private set; }
  
  [EqualityComparer(fromLocalProperty: nameof(NameComparer))]
  // Other examples:
  //[EqualityComparer(fromExternalProperty: "System.StringComparer.OrdinalIgnoreCase" /* not sure if I like this, since it'd need to know the assemblies to load */)]
  //[EqualityComparer(typeof(MyCustomStringComparer /* must have new() */)]
  //[EqualityComparerFactory(fromLocalMethod: nameof(CreateNameComparer))]
  public string Name { get; }

  bool IEquatable<TypeUnderTest>.Equals(TypeUnderTest other) => throw new ShouldBeReplacedByFodyEqualsException("Auto-implemented");
  
  private static IEqualityComparer<string> CreateNameComparer() => StringComparer.OrdinalIgnoreCase;
}

I like the fromExternalProperty the most -- since it reduces the code impact to literally 1 line to specify 1 thing (oh, how code should be!) but since I'm concerned about the making sure we pull in the right dependencies and prefer for usage errors to be caught by the compiler, I think that I officially back this approach (unless a maintainer of the project would feel versed enough in this stuff to validate a PR I send is correct/incorrect).

[Equals]
class TypeUnderTest : IEquatable<TypeUnderTest>
{
  private static readonly IEqualityComparer<String> NameComparer = StringComparer.OrdinalIgnoreCase;
  
  public int Number { get; private set; }
  
  [EqualityComparer(fromLocalProperty: nameof(NameComparer))]
  public string Name { get; }

  bool IEquatable<TypeUnderTest>.Equals(TypeUnderTest other) => throw new ShouldBeReplacedByFodyEqualsException("Auto-implemented");
}

Make an effort to fix the bug

Will be doing, but generally want to know that a proposal will be entertained before doing the work -- but important enough to me that I would fork the library if not :)

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