【发布时间】:2013-06-12 06:51:20
【问题描述】:
我有很多“乐趣”,试图理解为什么以下内容不起作用。在我的lein 项目的src 目录中启动nREPL 会话后,我执行了以下操作:
user> (require 'animals.core)
nil
user> (animals.core/-main)
"Welcome to Animals!"
nil
user> (require 'animals.animal)
nil
user> (extends? animals.animal/Animal animals.animal/Dog)
CompilerException java.lang.RuntimeException: No such var: animals.animal/Dog, compiling:(NO_SOURCE_PATH:1:1)
阅读 Lein 教程,我的基本理解是我应该这样放置我的源代码:
- src/
|____ animals/
|_______ core.clj
|_______ animal.clj
下面动物的内容是这样的:
(ns animals.animal)
(defprotocol Animal
"A protocol for animal move behavior."
(move [this] "Method to move."))
(defrecord Dog [name species]
Animal
(move [this] (str "The " (:name this) " walks on all fours.")))
(defrecord Human [name species]
Animal
(move [this] (str "The " (:name this) " walks on two legs.")))
(defrecord Arthropod [name species]
Animal
(move [this] (str "The " (:name this) " walks on eight legs.")))
(defrecord Insect [name species]
Animal
(move [this] (str "The " (:name this) " walks on six legs.")))
为什么在评估 Animal 没有错误时尝试评估 Dog 会导致运行时异常?如何正确地做到这一点?
【问题讨论】: