Discord.Net icon indicating copy to clipboard operation
Discord.Net copied to clipboard

[Feature] Embed comparison

Open Misha-133 opened this issue 2 years ago • 5 comments

This PR allows comparing embeds and its' child classes with each other

Changes:

Added GetHashCode(), Equals(), == operator and != operator to following classes:

  • Embed
  • EmbedFooter
  • EmbedAuthor
  • EmbedImage
  • EmbedThumbnail
  • EmbedField
  • EmbedBuilder
  • EmbedVideo
  • EmbedFooterBuilder
  • EmbedAuthorBuilder
  • EmbedFieldBuilder
  • EmbedProvider

Misha-133 avatar Jun 03 '22 21:06 Misha-133

GetHashCode shouldn't be overriden on mutable types. The hash will change every time the object is mutated, which will break code that relies on it (like dictionaries or hashsets).

Another problem I see is that an anonymous object is created every time a hash code is computed, creating unnecesary memory allocations. This can be fixed by using HashCode.Combine or tuples instead.

d4n3436 avatar Jun 04 '22 05:06 d4n3436

GetHashCode shouldn't be overriden on mutable types. The hash will change every time the object is mutated, which will break code that relies on it (like dictionaries or hashsets).

Another problem I see is that an anonymous object is created every time a hash code is computed, creating unnecesary memory allocations. This can be fixed by using HashCode.Combine or tuples instead.

I will look into first issue you've mentioned when get home

But the second one - first approach I tried used System.HashCode which did not compile for .NET Framework 4.6.1 & .NET Standard 2.0 There is nuget package Microsoft.Bcl.HashCode which brings support for HashCode in .NET Standard 2.0 but I didn't find any way to get it working in framework 4.6.1 so I had to use anonymous objects.

Misha-133 avatar Jun 04 '22 09:06 Misha-133

But the second one - first approach I tried used System.HashCode which did not compile for .NET Framework 4.6.1 & .NET Standard 2.0

Use tuples.

d4n3436 avatar Jun 04 '22 23:06 d4n3436

Issues should be solved now

Misha-133 avatar Jun 05 '22 20:06 Misha-133

Adding xmldoc addressing what is being compared would be desirable.

csmir avatar Jun 05 '22 20:06 csmir