uNmAnNeR
uNmAnNeR

Reputation: 610

Clojure extend Assotiative arity

I need to implement custom assoc, but it does not work with multiple arguments. It processes only first pair. It seems that it calls assoc directly, not via core assoc and then RT/assoc.

(def my-assoc (reify
                clojure.lang.Associative
                  (assoc [_ k v]
                    (println "assoc!!" k v))))

(assoc my-assoc :a 2 :b 3) ;; prints only :a 2

How it should be done to support multi arity?

Upvotes: 1

Views: 95

Answers (1)

ClojureMostly
ClojureMostly

Reputation: 4713

println returns nil. So return the original value:

(def my-assoc (reify
                clojure.lang.Associative
                (assoc [m k v]
                  (println "assoc!!" k v)
                  m)))

(assoc my-assoc :a 2 :b 3) ;; prints both

Upvotes: 3

Related Questions