Skip to content

Commit

Permalink
Use hash_code_combine and type-tag in hash_code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed Feb 7, 2023
1 parent f52d98f commit 2026002
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 3 additions & 6 deletions rhombus/data/equality.rhm
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import:
lib("racket/base.rkt"):
meta
expose: #{generate-temporaries}
lib("racket/unsafe/ops.rkt").#{unsafe-fx+/wraparound}
lib("racket/unsafe/ops.rkt").#{unsafe-fx*/wraparound}
lib("racket/base.rkt").#{eq-hash-code}


class_clause.macro 'equality: $key_expr; ...':
Expand All @@ -26,10 +25,8 @@ class_clause.macro 'equality: $key_expr; ...':
$('&& (recur(this . $key_id(), (other :~ PrivateForEquals) . $key_id()))') ...

private override method hash_code(recur):
let code = 1000003
let code:
#{unsafe-fx+/wraparound}(
#{unsafe-fx*/wraparound}(code, 31), recur(this . $key_id()))
let code = #{eq-hash-code}(PrivateForEquals)
let code: Equatable.hash_code_combine(code, recur(this . $key_id()))
...
code'

8 changes: 8 additions & 0 deletions rhombus/tests/equatable.rhm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Fail():
class OrderedTriple(fst, snd, thd):
equality: fst; snd; thd

class OrderedTriple2(fst, snd, thd):
equality: fst; snd; thd

check AnythingGoes() == AnythingGoes() ~is #true
check:
use_dynamic
Expand Down Expand Up @@ -83,6 +86,11 @@ check:
OrderedTriple("A", "B", "C").hash_code(fun (_): 0)
~raises "hash_code: no such field or method"

check: OrderedTriple("A", "B", "C") == OrderedTriple2("A", "B", "C") ~is #false
check: hc(OrderedTriple("A", "B", "C")) == hc(OrderedTriple2("A", "B", "C"))
~is #false
check: {OrderedTriple("A", "B", "C"), OrderedTriple2("A", "B", "C")}.length() ~is 2

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

0 comments on commit 2026002

Please sign in to comment.