Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 当两个deftype都位于不同的文件中时,如何将它们组合成一个新的deftype?_Clojure_Composition - Fatal编程技术网

Clojure 当两个deftype都位于不同的文件中时,如何将它们组合成一个新的deftype?

Clojure 当两个deftype都位于不同的文件中时,如何将它们组合成一个新的deftype?,clojure,composition,Clojure,Composition,repl中的以下工作: (defprotocol MyProtocol (foo [this])) (deftype A [] MyProtocol (foo [this] "a")) (deftype B [] MyProtocol (foo [this] "b")) (deftype C [] MyProtocol (foo [this] (str (foo (A.)) (foo (B.))))) 当我试图将每个实例移动到一个单独的文件以减少耦合时,在C上出现以

repl中的以下工作:

(defprotocol MyProtocol
  (foo [this]))
(deftype A []
  MyProtocol
  (foo [this] "a"))
(deftype B []
  MyProtocol
  (foo [this] "b"))
(deftype C []
  MyProtocol
  (foo [this] (str (foo (A.)) (foo (B.)))))
当我试图将每个实例移动到一个单独的文件以减少耦合时,在
C
上出现以下错误:“无法解析此上下文中的符号:foo”

布局示例:

;; my_protocol.clj
(ns my-protocol)
(defprotocol MyProtocol
  (foo [this]))

;; type_a.clj
(ns type-a
  (:require my-protocol :refer [MyProtocol])
(deftype A []
  MyProtocol
  (foo [this] "a"))

;; type_b.clj
(ns type-b
  (:require my-protocol :refer [MyProtocol])
(deftype B []
  MyProtocol
  (foo [this] "b"))

;; type_c.clj
(ns type-c
  (:import [type_a A]
           [type_b B])
  (:require my-protocol :refer [MyProtocol])
(deftype C []
  MyProtocol
  (foo [this] (str (foo (A.)) (foo (B.)))))    
您引用了协议,但从不引用
foo
,因此当您尝试调用
foo
时,编译器不知道您的意思。改为写:

(ns type-a
  (:require my-protocol :refer [MyProtocol foo])

我想你是说联姻。您通常希望具有高内聚性和低耦合性。我现在不能测试,但我会首先尝试完全限定
foo
,例如
my protocol/foo
。这是内聚/耦合的好观点。编辑,谢谢!在大多数教程中,我都不太清楚在使用defprotocol和deftype之后,名称空间是什么样子的。你有一个链接到一个好的,我们可以添加为子孙后代的链接?
(ns type-a
  (:require my-protocol :refer [MyProtocol foo])