如何解决Clojurescript defprotocol和defrecord在不同的名称空间中
这很好:
(ns ns1)
(defprotocol P
(foo [this a]))
(defrecord Foo [attr]
P
(foo [this a] a))
(println (foo (Foo. "test") "Works!"))
但是当我将协议移动到它自己的名称空间时,像这样:
(ns ns2)
(defprotocol P
(foo [this a]))
然后抛出Use of undeclared var ns1/foo
(ns ns1
(:require ns2 :refer [P]))
(defrecord Foo [attr]
P
(foo [this a] a))
(println (foo (Foo. "test") "Undeclared var?"))
我发现this的写作非常好,对于Clojure和Java来说很有意义。
是否可以使我上面的示例在clojurescript中工作?
更新:我知道我缺少一些超级简单的东西!记录下来,正如公认的答案中所述:我所需要的是像这样引用foo
:
(ns ns1
(:require ns2 :refer [P foo]))
(defrecord Foo [attr]
P
(foo [this a] a))
(println (foo (Foo. "test") "Works once again"))
解决方法
答案正确在您链接的博客文章中。您必须:refer
要使用的协议功能(或使用:require :as
来更明确地使用它们,就像如今的样式一样)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。