extend可以与Clojure 1.4.0中的defrecord一起使用吗?

extend可以与Clojure 1.4.0中的defrecord一起使用吗?,clojure,Clojure,我试图使用“extend”来定义Clojure中带有映射的记录方法。Clojure 1.4.0中的以下工作: (defprotocol PointMethods (add [self other]) (distance [self])) (defrecord Point [x y] PointMethods (add [self other] (Point. (+ (:x self) (:x other)) (+ (:y self) (:y other)))) (d

我试图使用“extend”来定义Clojure中带有映射的记录方法。Clojure 1.4.0中的以下工作:

(defprotocol PointMethods
  (add [self other])
  (distance [self]))

(defrecord Point [x y]
  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))
但这个版本失败了:

(defprotocol PointMethods
  (add [self other])
  (distance [self]))

(defrecord Point [x y])
(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: #'user/PointMethods found for class: user.Point, compiling:(records.clj:16)]

第二个版本有什么问题?

正如其他人在对您的代码的评论中所指出的那样,您的代码在Clojure 1.4.0中可以正常运行(我只是自己验证了它)。您可能会遇到某种IDE配置错误,或者可能存在一些过时的缓存字节码

我不认为它能解决您眼前的问题,但您在一个地方将距离拼写为distance,这最终会导致一个新问题。我刚刚复制了代码并将其粘贴到运行clojure 1.4.0的新REPL中,它对我有效(除了Amalloy的更正)。我刚刚在clojure 1.5.0中尝试了它,我的代码运行得很好。然后我在新安装的1.4.0中试用了它,我的代码也运行良好。我认为这实际上可能是一个想法/La Clojure问题。它仍然不能在Clojure文件中工作,也不能在IDE中从repl中工作。