2

Probably the title is not 100% correct, but let me show you the issue:

(defmacro nvp!
  [n width height]
  `(q/defsketch (symbol (str (name '~n) "-viewport" ))
                  :title (name '~n))

In short: there's a macro called defsketch (it's part of the lib quil, but that does not matter). It's signature is basically defsketch [applet-name & options], and it creates something and binds it to a var called applet-name. For some reason, I want to wrap this into another macro that -- among other things -- takes an applet-name parameter, extends that name into applet-name-viewport and passes that to defsketch. I, however, am unable to figure out how to do that correctly (using macroexpand on the code above ((pprint (macroexpand(nvp test-name 500 500))), I get

(def(clojure.core/symbol (clojure.core/str (clojure.core/name 'my-namespace.core/test-name) "-viewport"))
 (quil.applet/applet
 :title (clojure.core/name 'my-namespace.core/test-name)))

(clojure.core/symbol (clojure.core/str (clojure.core/name 'my-namespace.core/test-name) "-viewport") -- this part looks good, but it should be evaluated somehow before passing it to the inner macro...

1 Answer 1

3

You need to unquote the form that generates the new symbol instead of the original symbol itself.

Here is a small example of how to accomplish this using a defn like macro for the inner macro:

(defmacro mydefn
  [name & body]
  `(defn ~name ~@body))

(defmacro defnview
  [n & body]
  `(mydefn ~(symbol (str (name n) "-viewport")) ~@body))

;; OR

(defmacro defnview
  [n & body]
  (let [n (symbol (str (name n) "-viewport"))]
    `(mydefn ~n ~@body)))

Example:

(defnview foo [a] a)
;; => #'user/foo-viewport

(foo-viewport 1)
;; => 1

(macroexpand '(defnview foo [a] a))
;; => (def foo-viewport (clojure.core/fn ([a] a)))
3
  • Thanks - it's not completely clear yet though. In your examples you are not calling a second macro -- I need do to that, and the argument of the second macro should be a mutated version of the argument of the outer macro.
    – Mate Varga
    Oct 8, 2014 at 16:16
  • @MateVarga it doesn't make a different since you are returning a new form - if that happens to be a macro, it will be expanded. I'll update my answer with an inner macro.
    – Kyle
    Oct 8, 2014 at 16:20
  • Thanks a lot, I got it now. I'm very new to clj and macros are a bit though after 2 weeks :)
    – Mate Varga
    Oct 8, 2014 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.