Skip to content

Instantly share code, notes, and snippets.

Created August 9, 2019 13:33
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Does with-redefs work for functions in cljs.core, e.g. like vector in this example?
;; Command to start ClojureScript REPL:
;; clj -Sdeps "{:deps {org.clojure/clojurescript {:mvn/version \"1.10.520\"}}}" -m cljs.main --repl-env node
(defn wrapme [wrapped-fn a another-fn]
(fn [& args]
(println "called wrapped fn")
(let [ret (apply wrapped-fn args)]
(apply another-fn a ret args)
(defn f1 [x y & args]
(println "f1 x=" x "y=" y "args=" (vec args)))
(def my-vector vector)
(defn other-f4 [p q r]
(println "f4 here")
(my-vector p q r))
;; this causes other-f4 to call wrapme'd version of my-vector
;; printing these msgs:
;; f4 here
;; called wrapped fn
;; f1 x= foo ...
(with-redefs [my-vector (wrapme my-vector "foo" f1)]
(other-f4 13 17 19))
(defn f4 [p q r]
(println "f4 here")
(vector p q r))
;; This only prints:
;; f4 here
;; so it must not be calling the wrapped version of the function, nor f1
(with-redefs [vector (wrapme vector "foo" f1)]
(f4 13 17 19))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment