Add member_eq builtin for member-wise struct comparisons#2801
Add member_eq builtin for member-wise struct comparisons#2801lerno merged 15 commits intoc3lang:masterfrom
member_eq builtin for member-wise struct comparisons#2801Conversation
|
I've been looking at this one a lot, but ultimately I think it is too complex to go into the stdlib. Let me try to explain why I am ambivalent:
All of these things are subtly nudging users of the C3 stdlib that having a structure that doesn't work with If it had been more "dumb", like "it does == on all members" then it's more understood to be convenience. Here instead it is semi-smart about things and then it looks like there's meaning in the choices. I think that a dumb "member_equals" that just did Finally, this being a macro is a bit dangerous. I think people will just see this and think "oh, I can use compare these things using the |
|
Feel free to discuss this with me on discord. |
|
Finally I'd like to add that the ambiguity of how to interpret struct fields is exactly why I didn't want this |
|
First of all, thanks for writing out your reasoning very clearly and taking the time to review this. When I originally submitted it, I had a few doubts about whether it would be suitable for such a generic purpose, and I suspected there would be a mix of broader problems and more nuances - but I was hoping these issues could push it toward an agreeable state that "just worked".
Ah, so my thoughts about doing something like this instead were opposite of reality: I thought that would be too dumb, but considering allowing most structs to "qualify" to use it (by merit of
As we know from my past contributions, "macro" is my middle name. 😃 Jokes aside, agreed. It stems from my own bad habits, and I had somehow rationalized not needing to wrap it in a generic function of some sort. My follow-up now becomes - would you like me to implement a simpler |
|
Yes, implement a simpler member_eq, and have a |
|
Like: |
|
Should we close this one then? |
|
The updates came out much simpler than the original PR. Any pending reviews notwithstanding, I'd say this is good to go. |
struct_eq builtin for member-wise struct comparisonsstruct_eq~~ member_eq builtin for member-wise struct comparisons
struct_eq~~ member_eq builtin for member-wise struct comparisonsmember_eq builtin for member-wise struct comparisons
|
Thank you! |
The original PR comment below is outdated.
member_eqis now the built-in of choice for simple==across all of a given type's members.See the conversation below.
Allows comparisons of input structures that don't already have an
==overload (and it will just compare them anyway if they do). This reduces the need to define@operator(==)overrides which just doreturn self.a == self.a && self.b == self.b && ...;. They can now become something like this:This is especially useful when
memcmp-style comparisons of structs are not.You can also use the variadic arguments list to supply strings in dot-notation to exclude certain fields from being compared. See the unit tests for more information about how to do this.
The implementation is a bit redundant because there are no flow-control statements at compile-time. This could certainly use a few extra reviewers to ensure it works as broadly as possible, but tests seem to show a good coverage/capability.