Clojure 三个名称空间中的协议、实现和调用方
我有三个文件/n,分别是Clojure 三个名称空间中的协议、实现和调用方,clojure,Clojure,我有三个文件/n,分别是world.clj、node.clj和protocols.clj。节点记录实现了一个名为movable的协议。然后我想从world.clj(它维护节点等的状态)调用它,但我不知道如何调用。我需要什么:需要在哪里 protocols.clj: (ns mesh.protocols) (defprotocol movable (move [this pos]) node.clj: (ns mesh.node (:require [mesh.protocols :r
world.clj
、node.clj
和protocols.clj
。节点
记录实现了一个名为movable
的协议。然后我想从world.clj
(它维护节点等的状态)调用它,但我不知道如何调用。我需要什么:需要在哪里
protocols.clj:
(ns mesh.protocols)
(defprotocol movable
(move [this pos])
node.clj:
(ns mesh.node
(:require [mesh.protocols :refer [movable]]))
(defrecord Node [...]
movable
(move [this pos] ...))
world.clj:
(ns mesh.world
(:require ???))
(defn update-world [world]
...
(move node new-pos))
在world.clj
中,我需要什么才能在节点中调用移动的实现?根据我的尝试,我会遇到各种异常,如下面所示
- 线程“main”java.lang.RuntimeException中的异常:无法解析符号:在此上下文中移动,编译:(mesh/world.clj:13:29)
线程“main”java.lang.IllegalAccessError中的异常:移动
不存在,正在编译:(mesh/world.clj:1:1)
是否可以使用正确的:require
解决此问题,或者我是否需要移动东西?在这种情况下,您建议我如何组织这些内容?defprotocol
(除其他外)在封闭的命名空间中定义函数。客户端只是调用这些“多态”函数,就像调用任何常规函数一样
(ns mesh.world
(:require [mesh.protocols :as meshp]))
(defn update-world [world]
...
(meshp/move node new-pos))