gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:30:46 26 4
gpt4 key购买 nike

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])

您引用了协议(protocol),但从未引用过 foo,因此当您尝试调用 foo 时,编译器不知道您的意思。而是写:

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

关于clojure - 当两个 deftype 都位于不同的文件中时,如何将它们组合成一个新的 deftype?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37552231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com