macroexpand-1

added
1.0

ns
clojure.core

type
function

(macroexpand-1 form)

If form represents a macro form, returns its expansion,
else returns form.

                user=> (macroexpand-1 '(defstruct mystruct[a b]))
(def mystruct (clojure.core/create-struct [a b]))

            
                user=> (macroexpand-1 '(-> c (+ 3) (* 2)))
(clojure.core/-> (clojure.core/-> c (+ 3)) (* 2))
            
                ; When testing macro expansion in a file instead of at the REPL, 
; please note that`macroexpand-1` will not work inside of a unit test:

(defmacro iiinc [x]
  `(+ 3 ~x))

; This works when not in (deftest ...)
(println (macroexpand-1 '(iiinc 2))) ;=> (clojure.core/+ 3 2)

(deftest t-stuff
  ; But it is broken inside (deftest ...)
  (println (macroexpand-1 '(iiinc 2))) ;=> (iiinc 2)

  ; However, we can use the macro itself fine in our tests
  (println (iiinc 2))    ;=> 5
  (is (= 5 (iiinc 2))))  ;=> unit test passes

; Also, as the previous examples show, please remember that 
; you must quote the form you are providing to `macroexpand-1`.