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

Add an equality macro #270

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Add an equality macro #270

wants to merge 1 commit into from

Conversation

jackfirth
Copy link
Sponsor Collaborator

@jackfirth jackfirth commented Jan 9, 2023

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:

class Foo(a, b, c):
  equality:
    a
    b
    c

Expands into this, roughly:

class Foo(a, b, c):
  implements Equatable

  override method equals(other):
    (other is_a Foo)
      && (a == other.a)
      && (b == other.b)
      && (c == other.c)

  override method hashCode():
    let code = 1000003
    let code = 31 * code + a.hashCode()
    let code = 31 * code + b.hashCode()
    let code = 31 * code + c.hashCode()
    code

You can pass arbitrary expressions into equality, not just fields, so this works:

class TwoElementIntSet(a :: Integer, b :: Integer):
  equality:
    min(a, b)
    max(a, b)

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.

@jackfirth jackfirth added the libraries This is for issues related to Rhombus libraries label Jan 9, 2023
@hashim-hivery
Copy link

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

class Foo(a, b, c):
  equality: [a, b, c]

@AlexKnauth
Copy link
Member

class Foo(a, b, c):
  equality:
    a
    b
    c

is an equivalent tree to

class Foo(a, b, c):
  equality: a; b; c


override method equals(other):
(other is_a PrivateForEquals)
$$('&& (this . $key_id() == (other -: PrivateForEquals) . $key_id())') ...
Copy link
Member

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.

Copy link
Sponsor Collaborator Author

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.

Copy link
Member

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.

Copy link
Sponsor Collaborator Author

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

@Lazerbeak12345
Copy link

Lazerbeak12345 commented Jan 12, 2023

What are the magic numbers here doing?

More specifically; why these numbers?

@AlexKnauth
Copy link
Member

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 hash-code-combine in racket/src/cs/rumble/hash-code.ss.

@jackfirth
Copy link
Sponsor Collaborator Author

jackfirth commented Jan 13, 2023

For the 31 constant, see this stackoverflow post. For the 1000003 constant, see this one. I just copied what @AutoValue does since it seemed fine in practice. I'm guessing the reason for picking a large starting constant is to avoid hash codes for small record types being biased to small-ish positive integers.

@sorawee
Copy link
Contributor

sorawee commented Jan 13, 2023

Effective Java uses 1000003 in the AutoValue section without using 31 and 1000003 is used as a multiplier. On the other hand, the hashCode recipe section uses 31 without using 1000003. Using both 1000003 and 31 with 1000003 as the initial value is probably fine, but seems unnecessary.

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 hash-code-combine would be better.

Copy link

@Lazerbeak12345 Lazerbeak12345 left a 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.

@AlexKnauth
Copy link
Member

For hash-code-combine, I've made a PR to racket here: racket/racket#4546

@AlexKnauth
Copy link
Member

Some updates for recur and hash-code-combine: #274

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libraries This is for issues related to Rhombus libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants