Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ SCI is used in [babashka](https://github.com/babashka/babashka),
[joyride](https://github.com/BetterThanTomorrow/joyride/) and many
[other](https://github.com/babashka/sci#projects-using-sci) projects.

## Unreleased

- Support `hashCode` on `deftype`

## 0.11.50 (2025-12-23)

- Add support for `:refer-global` and `:require-global`
Expand Down
8 changes: 8 additions & 0 deletions src/sci/impl/deftype.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
(defmethod equals :default [this other]
(identical? this other))))

#?(:clj
(do
(defmulti hashCode types/type-impl)
(defmethod hashCode :default [this]
(System/identityHashCode this))))

(defn clojure-str [v]
;; #object[user.Foo 0x743e63ce "user.Foo@743e63ce"]
(let [n (types/type-impl v)]
Expand All @@ -53,6 +59,8 @@
(to-string this))
#?(:clj (equals [this other]
(sci.impl.deftype/equals this other)))
#?(:clj (hashCode [this]
(sci.impl.deftype/hashCode this)))

sci.impl.types/SciTypeInstance
(-get-type [_]
Expand Down
3 changes: 2 additions & 1 deletion src/sci/impl/namespaces.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,8 @@
{:obj (sci.lang/->Namespace 'sci.impl.deftype nil)
:private true
'toString sci.impl.deftype/to-string
#?@(:clj ['equals sci.impl.deftype/equals])
#?@(:clj ['equals sci.impl.deftype/equals
'hashCode sci.impl.deftype/hashCode])
'-create-type -create-type
'->type-impl sci.impl.deftype/->type-impl
'-inner-impl sci.impl.types/getVal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns sci.records-test
(ns sci.defrecords-and-defype-test
(:require
[clojure.string :as str]
[clojure.test :refer [are deftest is testing]]
Expand Down Expand Up @@ -223,7 +223,9 @@
(is (true? (tu/eval* "(deftype Foo []) (instance? Foo (->Foo))" {})))
(is (true? (tu/eval* "(ns bar) (deftype Foo []) (ns foo (:import [bar Foo])) (instance? Foo (Foo.))" {})))
(is (= "dude" (tu/eval* "(deftype Dude [] Object (toString [_] \"dude\")) (str (->Dude))" {})))
#?(: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))]" {})))))
#?(: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))]" {}))))
#?(:clj (is (true? (tu/eval* "(deftype Dude [x] Object (hashCode [_] 1))
(deftype Dude2 [x]) (and (= 1 (hash (Dude. 1337))) (not= 1 (hash (Dude2.))))" {})))))

(deftest equiv-test
(let [prog "(defrecord Foo [a]) (defrecord Bar [a]) [(= (->Foo 1) (->Foo 1)) (= (->Foo 1) (->Bar 1)) (= (->Foo 1) {:a 1})]"]
Expand Down
2 changes: 1 addition & 1 deletion test/sci/test_runner.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[sci.parse-test]
[sci.protocols-test]
[sci.read-test]
[sci.records-test]
[sci.defrecords-and-defype-test]
[sci.reify-test]
[sci.repl-test]
[sci.test-utils :refer [planck-env?]]
Expand Down
Loading