-
-
Notifications
You must be signed in to change notification settings - Fork 65
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
Add an equality macro #270
base: master
Are you sure you want to change the base?
Conversation
Are the a b c on separate lines, a list? If not, would they be nicer in a list? Then they can be in one line
|
is an equivalent tree to
|
|
||
override method equals(other): | ||
(other is_a PrivateForEquals) | ||
$$('&& (this . $key_id() == (other -: PrivateForEquals) . $key_id())') ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok if I change $$
to $&
(so this code would need to be updated)?
I think $&
connects better to a splicing &
in repetitions. That is, $&
is a combination of an escape and a splice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, fine by me. I like $&
better too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And now it's just $
. Unlike S-expressions, a group or a list of terms doesn't correspond to a term, so $
can just infer splicing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice, that's even better
What are the magic numbers here doing? More specifically; why these numbers? |
It would be good to replace the magic numbers in the hash-code method with a call to a hash-code-combine function similar to |
For the 31 constant, see this stackoverflow post. For the 1000003 constant, see this one. I just copied what |
Effective Java uses 1000003 in the While it might be the case that 1000003 and 31 work, I really dislike how Effective Java says "trust me, just do it and it will be fine" without giving any justification. The inconsistency (using 1000003 in a place and 31 in another place) is also baffling. I agree with @AlexKnauth that exposing and calling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into as many edge cases that came to mind without actually running this, and aside from recommendation to make use of a pre-existing hash method (preventing code duplication and reducing magic number use), I can't see any other issues.
We need to be sure to document that the other
value is first getting passed through (_ -: PrivateForEquals)
before getting the properties, and everything that entails. But that's for once we actually start writing docs. Unsure if docs would belong in this pr.
For |
Some updates for recur and hash-code-combine: #274 |
This is a draft PR I'm leaving up as a reference. It implements a hypothetical
equality:
macro for specifying what components of a class are relevant when comparing its instances for equality. This code:Expands into this, roughly:
You can pass arbitrary expressions into
equality
, not just fields, so this works:I'm drafting this PR for reference in future discussions. I'm not sure this is the way to go, but I did want to make this code available. Consider it a proof of concept.