I am trying to use 'extend' to define recording methods with a map in Clojure. In Clojure 1.4.0:
the following is true:
(defprotocol PointMethods (add [self other]) (distance [self])) (defrecord Point [xy] PointMethods (add [self other] (Point. (+ (:x self) (:x other)) (+ (:y self) (:y other)))) (distance [self] (Math/sqrt (+ (* (:x self) (:x self)) (* (:y self) (:y self)))))) (def p1 (Point. 2 3)) (def p2 (Point. 1 1)) (def p3 (add p1 p2)) (println p3) (println (distance p3))
But this version does not work:
(defprotocol PointMethods (add [self other]) (distance [self])) (defrecord Point [xy]) (extend Point PointMethods {:add (fn [self other] (Point. (+ (:x self) (:x other)) (+ (:y self) (:y other)))) :distance (fn [self] (Math/sqrt (+ (* (:x self) (:x self)) (* (:y self) (:y self)))))}) (def p1 (Point. 2 3)) (def p2 (Point. 1 1)) (def p3 (add p1 p2)) (println p3) (println (distance p3)) Clojure Compiler: java.lang.IllegalArgumentException: No implementation of method: :add of protocol:
What happened to the second version?
source share