this post was submitted on 15 Nov 2024
227 points (93.8% liked)

Programmer Humor

39094 readers
117 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] Gonzako@lemmy.world 4 points 1 year ago (2 children)

I've only had to implement equality in C# but that didn't seem that hard of a problem. you just expand the operator = function

[–] porous_grey_matter@lemmy.ml 9 points 1 year ago (2 children)

It's not hard, just if you're doing it for a struct with a lot of fields it's a lot of boilerplate

[–] GetOffMyLan@programming.dev 1 points 1 year ago* (last edited 1 year ago)

I just use the HashCode class and compare the results.

Pretty sure there's a source generator for it as well nowadays.

[–] Deckweiss@lemmy.world -5 points 1 year ago* (last edited 1 year ago) (2 children)

My IDE can do that for me. And it was able to do that pre AI boom. Yes, the code ends up more verbose, but I just collapse it.

So from a modern dev UX perspective, this shouldn't be a major difference.

[–] porous_grey_matter@lemmy.ml 3 points 1 year ago

Even if the tool works perfectly, you have to run it every time you change something. It's not the end of the world, but it's still much nicer to just have a macro to derive it at compile time.

[–] kazaika@lemmy.world 1 points 1 year ago

What if youre working with library types? The problem is not not you compare a bunch of fields but that the implementation on those members is most likely bad.

[–] copygirl@lemmy.blahaj.zone 5 points 1 year ago (1 children)

Then you should also override Equals(object), GetHashCode, and implement IEquatable<T>.

Thankfully a lot of the usual boilerplate code can be avoided using a record class or struct:

public record Person(string Name, uint Age);
[–] Gonzako@lemmy.world 1 points 1 year ago

Oh well, It does show how little I do have to actually use that. It just hasn't come up that much