Hamza Yerlikaya
Hamza Yerlikaya

Reputation: 49339

Applying a macro to a form without defining the macro

Is there a way to expand a macro without actually defining it? My situation is that I have a bunch of macros that may/may not override clojure macros, I need to a apply those macros to a list of s-expressions?

Upvotes: 4

Views: 183

Answers (2)

amalloy
amalloy

Reputation: 92087

Macroexpand can't see macros introduced by macrolet. They only exist at compilation time, and macroexpand (when you call it directly) operates at run time. Your test only works because you defined when the same way clojure.core does. But, macrolet will solve your problem as long as you don't want to expand at run time.

Upvotes: 4

Hamza Yerlikaya
Hamza Yerlikaya

Reputation: 49339

Digging through contrib, found what I was looking for,


(use ' clojure.contrib.macro-utils)
(macrolet [(when [test & body]
             (list 'if test (cons 'do body)))]

          (macroexpand '(when true 4)))

(if true (do 4))

Upvotes: 2

Related Questions