No single method in protocol error with swap! call 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))) 

It leads to an error:

An exception in the thread "main" java.lang.IllegalArgumentException: there is no single method: interface damage: protocols_records_learning.core.Hit_point found for function: protocol corruption: Hit-points (core.clj: 34)

Can someone explain this error, and what causes it, and how to change the atom correctly?

+4
source share
1 answer

That should work.

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

Please note: the removed pair of guys around the damage. The error message is a clumsy attempt to tell you that Clojure did not find the damage version with arch 1. The paranoids around the damage try to do just that: do damage with one argument (2).

Improving error messages is an ongoing task that has come down to protocols.

+5
source

All Articles