Добавление методов экземпляра

Расширения

(defrecord Point [x y])

;; протокол можно прикрепить к чужому типу, чьим исходным кодом не владеют,
;; и это «метод расширения» в Clojure
(defprotocol Distance
  (distance-to [this other]))

(extend-protocol Distance
  Point
  (distance-to [p other]
    (Math/hypot (- (:x p) (:x other))
                (- (:y p) (:y other)))))

(def distance (distance-to (->Point 1.0 2.0) (->Point 2.0 3.0)))
;; distance is 1.4142
(println "distance is" (format "%.4f" distance))

;; тот же приём работает и с Java-классом, в том числе со String
(extend-protocol Distance
  String
  (distance-to [s other] (abs (- (count s) (count other)))))

(println "distance is" (distance-to "abcde" "ab"))