交换中没有单一的协议错误方法!打电话给Clojure

时间:2011-07-14 05:56:16

标签: clojure

(ns protocols-records-learning.core)

(defprotocol Hit-points
  "Able to be harmed by environment interaction."
  (hit? [creature hit-roll] "Checks to see if hit.")
  (damage [creature damage-roll] "Damages target by damage-roll, negated by per-implementation factors.")
  (heal [creature heal-roll] "Heals creature by specified amount."))

(defrecord Human [ac, health, max-health]
  Hit-points
  (hit? [creature hit-roll] (>= hit-roll ac))
  (damage [creature damage-roll] (if (pos? damage-roll) (Human. ac (- health damage-roll) max-health)))
  (heal [creature heal-roll] (if (pos? heal-roll)
                    (if (>= max-health (+ heal-roll health))
                      (Human. ac max-health max-health)
                      (Human. ac (+ heal-roll health) max-health)))))

(def ryan (atom (Human. 10 4 4)))

(defn hurt-ryan
  "Damage Ryan by two points."
  [ryan]
  (swap! ryan (damage 2)))

导致错误:

线程“main”中的异常java.lang.IllegalArgumentException:没有单一方法:接口损坏:为功能找到的protocols_records_learning.core.Hit_points:协议损坏:命中点(core.clj:34)

有人可以解释这个错误,导致它的原因,以及如何正确地改变原子?

1 个答案:

答案 0 :(得分:5)

这应该有用。

(defn hurt-ryan
  "Damage Ryan by two points."
  [ryan]
  (swap! ryan damage 2))
注意,被拆除的一对parens周围的损坏。错误消息是一个笨重的尝试,告诉你Clojure没有找到一个版本的伤害与1的arity。损坏周围的parens试图做到这一点:用一个参数调用损害(2)。

错误消息的改进是一项持续的任务,已经达到了协议。