Search code examples
clojureextendassociativereify

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?


Solution

  • 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