Search code examples
macrosclojuredeftype

Clojure - How to refer deftype's variables in macros?


When I do

(defmacro my-deftype [& code] `(deftype ~@code (toString [this] var1)))
(my-deftype Qqq [var1] Object)

it tells CompilerException ... No such var: mynamespace/var1

How to refer deftype's variables in macros properly? I want macros to provide a template for methods and avoid mentioning all deftype's variables in each method.


Solution

  • Namespace expansion should be prevented by ~':

    (defmacro my-deftype [& code] `(deftype ~@code (toString [~'this] ~'var1)))
    (my-deftype Qqq [var1] Object)