我有一个宏:
(defmacro widget [msg-type value & app-key]
`(defrecord ~msg-type [~value]
Message
(~'process-message [msg# app#]
(let [state# (~@app-key app#)]
(dissoc
(->>
(merge state# msg#)
(assoc app# ~@app-key))
:errors)))))Message是在结束语依赖中定义的带有process-message函数的协议。
当我尝试像这样使用widget时
(ns my.cljs.ns
(:require-macros [my.ns.macros :as macro])
(:require [petrol.core :refer [Message]]))
(macro/widget A-Record a-field :a-key)我知道这个错误:
Bad method signature in protocol implementation,
my.ns.macros/Message does not declare method called
process-message ...如何使消息引用petrol/Message而不是my.ns.macros/Message
发布于 2016-02-11 03:39:52
您需要神秘的~‘操作符的强大功能:)我看到您已经为进程消息调用了它,所以您可能已经知道了为什么;但是为了回答的目的,反勾框中的内容会完全限定名称空间,就像计算引号将文字符号放置到位一样。
(macroexpand-1 '(widget :a :b))该错误消息表明,如果要避免将当前ns附加到它,则需要~'Message。
然而,使用汽油命名空间对消息进行完全限定将是一个很好的移动,IMO。
petrol.core/Message这样,您就不需要依赖在ns声明中引用它了。注意,你也不需要这样做。
另外,我也很小心(~@app-key app#),因为app-key是可选的.你不可能通过任何叫做#app的东西,这听起来不像是你想要发生的事情。同样地,超过一个人似乎也是如此。也许这应该是必需的对白?
https://stackoverflow.com/questions/35329220
复制相似问题