Skip to content

Commit f957c69

Browse files
thomasmulvaneyswannodette
authored andcommitted
CLJS-2079: Records and maps are not equal
1 parent 3438f20 commit f957c69

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

src/main/cljs/cljs/core.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5833,7 +5833,7 @@ reduces them without incurring seq initialization"
58335833
false."
58345834
[x y]
58355835
(boolean
5836-
(when (map? y)
5836+
(when (and (map? y) (not (record? y)))
58375837
; assume all maps are counted
58385838
(when (== (count x) (count y))
58395839
(every? (fn [xkv] (= (get y (first xkv) never-equiv)
@@ -6338,7 +6338,7 @@ reduces them without incurring seq initialization"
63386338

63396339
IEquiv
63406340
(-equiv [coll other]
6341-
(if (implements? IMap other)
6341+
(if (and (implements? IMap other) (not (record? other)))
63426342
(let [alen (alength arr)
63436343
^not-native other other]
63446344
(if (== cnt (-count other))

src/main/clojure/cljs/core.cljc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,13 +1748,17 @@
17481748
'IHash
17491749
`(~'-hash [this#] (caching-hash this# ~'hash-imap ~'__hash))
17501750
'IEquiv
1751-
`(~'-equiv [this# other#]
1752-
(if (and other#
1753-
(identical? (.-constructor this#)
1754-
(.-constructor other#))
1755-
(equiv-map this# other#))
1756-
true
1757-
false))
1751+
(let [this (gensym 'this) other (gensym 'other)]
1752+
`(~'-equiv [~this ~other]
1753+
(and (some? ~other)
1754+
(identical? (.-constructor ~this)
1755+
(.-constructor ~other))
1756+
~@(map (fn [field]
1757+
`(= (.. ~this ~(to-property field))
1758+
(.. ~other ~(to-property field))))
1759+
base-fields)
1760+
(= (.-__extmap ~this)
1761+
(.-__extmap ~other)))))
17581762
'IMeta
17591763
`(~'-meta [this#] ~'__meta)
17601764
'IWithMeta
@@ -1821,7 +1825,7 @@
18211825
ks (map keyword fields)
18221826
getters (map (core/fn [k] `(~k ~ms)) ks)]
18231827
`(defn ~fn-name [~ms]
1824-
(new ~rname ~@getters nil (dissoc ~ms ~@ks) nil))))
1828+
(new ~rname ~@getters nil (not-empty (dissoc ~ms ~@ks)) nil))))
18251829

18261830
(core/defmacro defrecord
18271831
"(defrecord name [fields*] options* specs*)

src/test/cljs/cljs/core_test.cljs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,25 @@
12811281
(let [sm (sorted-map 1 1, 2 2, 3 3, 4 4, 5 5, 6 6, 7 7)]
12821282
(is (= [1 2 3 4] (reduce-kv (fn [m k v] (if (= 5 k) (reduced m) (conj m k))) [] sm))))))
12831283

1284+
(defrecord CLJS2079 [a b])
1285+
1286+
(deftest test-cljs-2079
1287+
(testing "Records and maps should not be equal"
1288+
(let [am (array-map :a 1 :b 2)
1289+
hm (hash-map :a 1 :b 2)
1290+
sm (sorted-map :a 1 :b 2)
1291+
r (->CLJS2079 1 2)]
1292+
(is (= am hm sm))
1293+
1294+
(is (not= r am))
1295+
(is (not= am r))
1296+
1297+
(is (not= r hm))
1298+
(is (not= hm r))
1299+
1300+
(is (not= r sm))
1301+
(is (not= sm r)))))
1302+
12841303
(comment
12851304
;; ObjMap
12861305
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)