In the compojure library in the core namespace, I see the following form:
(defn- compile-route
"Compile a route in the form (method path & body) into a function."
[method route bindings body]
`(#'if-method ~method
(#'if-route ~(prepare-route route)
(fn [request#]
(let-request [~bindings request#]
(render (do ~@body) request#))))))
and
(defmacro GET "Generate a GET route."
[path args & body]
(compile-route :get path args body))
Further up in the file, the if-method
and if-route
functions are defined with defn-
s.
I don't understand the meaning of the #'
in this compile-route
function though. The docs for (var ...)
says:
The symbol must resolve to a var, and the Var object itself (not its value) is returned. The reader macro #'x expands to (var x).
But to me, in the context of what is happening (ie being called from a defmacro), it just sounds like it means the value of the symbol will be returned, which is the same as what substitutability sounds like:
(def x 5)
(+ x 7)
-> 12
ie, (+ x 7)
expands to, or is the same as, (+ 5 7)
What am I missing here?
After looking at this for a while, I'm starting to suspect that it has something to do with the fact that the if-method
and if-route
functions are actually private ((defn- if-route ...)
).
Also, for macros, when you do the back-tick quote (" ` ") you are actually getting the fully-namespace-specified symbol in the final expansion:
`(+ 2 3)
would expand to
(clojure.core/+ 2 3)
Since these methods are private, they won't be accessible in the normal expansion, therefore the var function is used, to get to the symbol that holds a reference to the actual function that must be invoked at this point in the expansion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With