Variable passed to macro gets resolved in wrong namespace?

笑着哭i 提交于 2019-12-01 04:17:18
Michiel Borkent

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
))

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 seems, that noir is not meant to be used this way, because it takes the method argument and transforms it to the symbol in compojure.core (see https://github.com/ibdknox/noir/blob/master/src/noir/core.clj#L36). It means, that it doesn't expect a variable in this place, only literals. So I don't think you can do anything about that, except post an issue to noir...

If we look through noir/core.clj file (source), find parse-route function and reason what it does with its method argument (it is called action there), we could find that method keyword is converted to string, uppercased and resolved in compojure.core namespace. All this is done during macro expansion time. So it is not possible to use variable instead of keyword without altering noir code.

What about passing my-method along with namespace it is in:

(defpage [myns/my-method "some/url"] [data]
;;
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!