Skip to content

Commit 03db904

Browse files
authored
Support hashCode on deftype (#1017)
1 parent 3d3468a commit 03db904

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ SCI is used in [babashka](https://github.com/babashka/babashka),
1010
[joyride](https://github.com/BetterThanTomorrow/joyride/) and many
1111
[other](https://github.com/babashka/sci#projects-using-sci) projects.
1212

13+
## Unreleased
14+
15+
- Support `hashCode` on `deftype`
16+
1317
## 0.11.50 (2025-12-23)
1418

1519
- Add support for `:refer-global` and `:require-global`

src/sci/impl/deftype.cljc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
(defmethod equals :default [this other]
3636
(identical? this other))))
3737

38+
#?(:clj
39+
(do
40+
(defmulti hashCode types/type-impl)
41+
(defmethod hashCode :default [this]
42+
(System/identityHashCode this))))
43+
3844
(defn clojure-str [v]
3945
;; #object[user.Foo 0x743e63ce "user.Foo@743e63ce"]
4046
(let [n (types/type-impl v)]
@@ -53,6 +59,8 @@
5359
(to-string this))
5460
#?(:clj (equals [this other]
5561
(sci.impl.deftype/equals this other)))
62+
#?(:clj (hashCode [this]
63+
(sci.impl.deftype/hashCode this)))
5664

5765
sci.impl.types/SciTypeInstance
5866
(-get-type [_]

src/sci/impl/namespaces.cljc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,8 @@
837837
{:obj (sci.lang/->Namespace 'sci.impl.deftype nil)
838838
:private true
839839
'toString sci.impl.deftype/to-string
840-
#?@(:clj ['equals sci.impl.deftype/equals])
840+
#?@(:clj ['equals sci.impl.deftype/equals
841+
'hashCode sci.impl.deftype/hashCode])
841842
'-create-type -create-type
842843
'->type-impl sci.impl.deftype/->type-impl
843844
'-inner-impl sci.impl.types/getVal
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns sci.records-test
1+
(ns sci.defrecords-and-defype-test
22
(:require
33
[clojure.string :as str]
44
[clojure.test :refer [are deftest is testing]]
@@ -223,7 +223,9 @@
223223
(is (true? (tu/eval* "(deftype Foo []) (instance? Foo (->Foo))" {})))
224224
(is (true? (tu/eval* "(ns bar) (deftype Foo []) (ns foo (:import [bar Foo])) (instance? Foo (Foo.))" {})))
225225
(is (= "dude" (tu/eval* "(deftype Dude [] Object (toString [_] \"dude\")) (str (->Dude))" {})))
226-
#?(:clj (is (= [true false] (tu/eval* "(deftype Dude [x] Object (toString [_] (str x)) (equals [this other] (= (str this) (str other)))) [(= (->Dude 1) (->Dude 1)) (= (->Dude 1) (->Dude 2))]" {})))))
226+
#?(:clj (is (= [true false] (tu/eval* "(deftype Dude [x] Object (toString [_] (str x)) (equals [this other] (= (str this) (str other)))) [(= (->Dude 1) (->Dude 1)) (= (->Dude 1) (->Dude 2))]" {}))))
227+
#?(:clj (is (true? (tu/eval* "(deftype Dude [x] Object (hashCode [_] 1))
228+
(deftype Dude2 [x]) (and (= 1 (hash (Dude. 1337))) (not= 1 (hash (Dude2.))))" {})))))
227229

228230
(deftest equiv-test
229231
(let [prog "(defrecord Foo [a]) (defrecord Bar [a]) [(= (->Foo 1) (->Foo 1)) (= (->Foo 1) (->Bar 1)) (= (->Foo 1) {:a 1})]"]

test/sci/test_runner.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[sci.parse-test]
1515
[sci.protocols-test]
1616
[sci.read-test]
17-
[sci.records-test]
17+
[sci.defrecords-and-defype-test]
1818
[sci.reify-test]
1919
[sci.repl-test]
2020
[sci.test-utils :refer [planck-env?]]

0 commit comments

Comments
 (0)