gpt4 book ai didi

syntax - Clojure - deftype 被忽略 - 无法在测试中解析类名

转载 作者:行者123 更新时间:2023-12-04 19:41:34 25 4
gpt4 key购买 nike

我正在测试 deftypedefprotocol在 Clojure 中,但我有点不高兴。

我正在使用莱宁根。我的核心文件( src/core.clj )如下所示:

(defprotocol Speaker
(say [speaker message]))

(deftype Person [name]
Speaker
(say [_ message] (str name ": " message)))

我的测试文件( test/core.clj )如下所示:
(deftest people-can-talk
(is (= "Peter: hello" (say (Person. "Peter") "hello"))))

当我执行该测试(使用 lein test )时,出现以下错误:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to resolve classname: Person, compiling:(my-project/test/core.clj:2)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6416)
at clojure.lang.Compiler.analyze(Compiler.java:6216)

我相信它告诉我 Person没有定义。但它是!如果不是,Clojure 不会抛出错误吗?我遗漏了一些明显的语法错误吗?

我正在使用 clojure 1.3.0 。

最佳答案

要在另一个包/文件中使用由 deftype 定义的类,您需要(导入)它们,或提供完整的包名称,如 (core.Person. "Peter") - 或者无论 core.clj 的命名空间是什么。

此外,“lein test”仅加载测试文件。如果要引用另一个文件中的任何内容,则需要在测试文件中使用或要求该文件。

关于syntax - Clojure - deftype 被忽略 - 无法在测试中解析类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8201933/

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