1
votes

Let’s say that I want to define a macro called defsomething such that this:

(defspecial a x)

…expands into this:

(def a (f `a x))

That syntax-quotation `a there is the tricky part. I don’t see how I can attach the current namespace to the symbol properly so that it behaves like the syntax-quote. For instance:

(defmacro defspecial [var-symbol expr]
  `(def ~var-symbol (f '~var-symbol ~expr)))

(defspecial a x)

…expands into:

(def a (f 'a x))

…which is close, but that second a is not namespace-qualified.

I know I could use *ns*:

(defmacro defspecial [var-symbol expr]
  `(def ~var-symbol (f (symbol (name (ns-name *ns*)) (name '~var-symbol)) ~expr)))

…but I don’t like that; not only is it ugly, *ns* is rebindable, so this would be possible:

(binding [*ns* (the-ns 'clojure.core)]
  (defspecial a 5))

Unfortunately, Clojure apparently has no syntax-quote form for ` like it has quote for '. And so, how should I best implement this macro, then?

1
` doesn't append the current namespace. It's hard to understand what you're trying to achieve without you giving in an example of what you'd like and why it doesn't work (I mean, I get that you want to place a syntax quote somewhere, I'm talking about what your macro is actually supposed to do).Cubic
The background’s that this is for a library that combines functions together to make parsers. One higher-level function (call it lbl) takes a function (call it p) and wraps it in another function that adds a sort of “label” to whatever p returns (for the parse functions’ syntax error handling). I’d like to make a version of def for defining parser functions, and I’d like it to automatically call lbl on the functions and label them with their vars’ names. Namespace qualification would be nice here, since I anticipate a lot of label collisions otherwise.jschoi

1 Answers

1
votes

Have you actually tried the last example you gave? The compiler's binding of *ns* happens at compile-time, as does the macroexpansion, so the run-time binding you do should have no effect on the behavior of defspecial.