类 / 继承

(defprotocol Shape
  (area [this]))

(defrecord Square [side])

;; extend-type attaches a protocol to a type from the OUTSIDE,
;; long after the type itself was written
(extend-type Square
  Shape
  (area [this] (let [s (:side this)] (* s s))))

;; and it works on types you do not own — even on String,
;; which no class-based language would let you extend
(extend-type String
  Shape
  (area [this] (count this)))

(println "area is" (area (->Square 5)))
(println "area is" (area "hello"))