Welcome! Please see the About page for a little more info on how this works.

+2 votes
in ClojureScript by

After discussing it on Slack, the following definitely feels like a bug. At the very least, it throws weirdly in CLJS while it is perfectly (and intuitively) fine in Clojure JVM.

(def tree
     (sorted-map 3
                 (sorted-map 1
                             {:a 'leaf-A})))
(def subtree
     (sorted-map 1
                 (sorted-map 100
                             {:b 'leaf-B})))
;; Fine, of course, but now...
(assoc tree
       3
       subtree)
;; Throws:   Error: Cannot compare :a to 100

Andy Fingerhut pointed to a difference in implementation. Clojure compares by reference and CLJS by value, forcing a deep comparison. Even then, I find the error cryptic and argue that logically speaking, Clojure makes sense.

Citing him, Clojure implementation:

https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentTreeMap.java#L132

And CLJS implementation:

https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L8857

Is there something we didn't see?

1 Answer

+1 vote
by

What is really cryptic is that other updates to nested sorted maps are just fine, as far as I experimented, even mixed with unsorted ones. Here, it specifically fails because there is a common subpath (ie. [3 1]).

Welcome to Clojure Q&A, where you can ask questions and receive answers from members of the Clojure community.
...