Skip to content

Instantly share code, notes, and snippets.

@jafingerhut
Created August 9, 2019 13:33
Show Gist options
  • Save jafingerhut/63a17a7b9562237954cf4be62bb9fae4 to your computer and use it in GitHub Desktop.
Save jafingerhut/63a17a7b9562237954cf4be62bb9fae4 to your computer and use it in GitHub Desktop.
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)
ret)))
(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