clojure:#'/(var ...)形式的用法是什么? [英] clojure: What's the use of the #'/(var ...) form?
问题描述
在核心命名空间的 compojure库中,我看到以下内容形式:
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#))))))
和
(defmacro GET "Generate a GET route."
[path args & body]
(compile-route :get path args body))
在文件中, if-method
和 if-route
函数用 defn -
定义。
Further up in the file, the if-method
and if-route
functions are defined with defn-
s.
我不明白 '
在编译路由
函数。 (var ...)
的文档说:
I don't understand the meaning of the #'
in this compile-route
function though. The docs for (var ...)
says:
符号必须解析到var,并返回Var对象本身(不是它的值)。读者宏#'x展开为(var x)。
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).
但对我来说,从defmacro调用),它只是听起来像这意味着符号的值将被返回,这与可替代性听起来像相同:
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
即(+ x 7)
(+ 5 7)
我在这里缺少什么?看了这一段时间后,我开始怀疑它与事实有关的事情,<$($)
What am I missing here?
推荐答案
c $ c> if-method 和 if-route
函数实际上是私有的((defn- if- 。)
)。
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)
会扩展到
(clojure.core/+ 2 3)
由于这些方法是私有的, ,因此使用var函数来获取保存对在扩展中的该点必须调用的实际函数的引用的符号。
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.
这篇关于clojure:#'/(var ...)形式的用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!