小编Jon*_*han的帖子

可以在Clojure 1.4.0中扩展与defrecord一起使用吗?

我试图使用'extend'在Clojure中使用map定义记录方法.以下适用于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))
Run Code Online (Sandbox Code Playgroud)

但是这个版本失败了:

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

(defrecord Point [x y])
(extend Point
  PointMethods
  {:add
   (fn [self …
Run Code Online (Sandbox Code Playgroud)

clojure

6
推荐指数
1
解决办法
619
查看次数

标签 统计

clojure ×1