传递给宏的变量在错误的命名空间中解析? [英] Variable passed to macro gets resolved in wrong namespace?
问题描述
Noir 宏 defpage
给我一些麻烦。我试图构造一个类似于这样的调用:
The Noir macro defpage
is giving me a little bit of trouble. I am trying to construct a call similar to this:
(defpage [:post "some/url"] [data]
;; some stuff...
)
关键字:post
我想使用一个变量,如下所示:
However, instead of using the keyword :post
I would like to use a variable, like this:
(def my-method :post)
(defpage [my-method "some/url"] [data]
;; some stuff...
)
$ b < c $ c>在 compojure.core
命名空间而不是我自己的,给我错误:
The problem is that when the macro expands, it wants to resolve the variable my-method
in the compojure.core
namespace instead of my own, giving me the error:
No such var: compojure.core/MY-METHOD
推荐答案
我在方法猜猜这是一个类似的问题:如何将clojure的doc函数应用于一系列函数
宏可以使用其args做任何想要的操作,因此传递裸体符号可能导致不可预测的结果。
I guess this is a similar problem to: How can I apply clojure's doc function to a sequence of functions A macro can do whatever it wants with its args, so passing a naked symbol in can result in unpredictable results.
一种解决方法,但它不漂亮:
A way to solve it, but it ain't pretty:
(eval (list 'defpage (vector my-method "some/url") '[data]
; some stuff
))
请注意,my-method不是一个文字,在进入eval之前首先在我们自己的命名空间中解析和求值。
Notice that my-method is not a literal here, so it gets resolved and evaluated in our own namespace first, before going into eval.
这篇关于传递给宏的变量在错误的命名空间中解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!