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 6, 2023
1 parent f8020a0 commit 06ef3ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions rhombus/data/equality.rhm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import:
lib("racket/base.rkt"):
meta
expose: #{generate-temporaries}
lib("racket/base.rkt").#{eq-hash-code}
lib("racket/hash-code.rkt").#{hash-code-combine}
lib("racket/unsafe/ops.rkt").#{unsafe-fx+/wraparound}
lib("racket/unsafe/ops.rkt").#{unsafe-fx*/wraparound}

Expand All @@ -26,10 +28,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: #{hash-code-combine}(code, recur(this . $key_id()))
...
code'

Expand Down
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 @@ -82,3 +85,8 @@ check:
use_dynamic
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

0 comments on commit 06ef3ec

Please sign in to comment.