当两个 deftype 都位于不同的文件中时,如何将它们组合成一个新的 deftype?
How do I compose two deftypes into a new deftype when they all live in different files?
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
上收到以下错误:"Unable to resolve symbol: foo in this context"
示例布局:
;; 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.)))))
(ns type-a
(:require my-protocol :refer [MyProtocol])
您引用了协议,但从未引用过 foo
,因此当您尝试调用 foo
时,编译器不知道您的意思。而是写:
(ns type-a
(:require my-protocol :refer [MyProtocol foo])
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
上收到以下错误:"Unable to resolve symbol: foo in this context"
示例布局:
;; 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.)))))
(ns type-a
(:require my-protocol :refer [MyProtocol])
您引用了协议,但从未引用过 foo
,因此当您尝试调用 foo
时,编译器不知道您的意思。而是写:
(ns type-a
(:require my-protocol :refer [MyProtocol foo])