ClojureDocs

导航

命名空间

extend-type

clojure.core

1.2 及更高版本可用 ()
  • (extend-type t & specs)
A macro that expands into an extend call. Useful when you are
supplying the definitions explicitly inline, extend-type
automatically creates the maps required by extend.  Propagates the
class as a type hint on the first argument of all fns.
 (extend-type MyType 
  Countable
    (cnt [c] ...)
  Foo
    (bar [x y] ...)
    (baz ([x] ...) ([x y & zs] ...)))
 expands into:
 (extend MyType
 Countable
   {:cnt (fn [c] ...)}
 Foo
   {:baz (fn ([x] ...) ([x y & zs] ...))
    :bar (fn [x y] ...)})
1 Example
;;; This is a library for the shopping result.

(defrecord Banana [qty])

;;; 'subtotal' differ from each fruit.

(defprotocol Fruit
  (subtotal [item]))

(extend-type Banana
  Fruit
  (subtotal [item]
    (* 158 (:qty item))))

;;; Please see the term of 'reify'.
See Also

Implementations of protocol methods can be provided using the extend construct: (extend AType ...

Added by Cosmi

Useful when you want to provide several implementations of the same protocol all at once. Takes a ...

Added by klauern

A protocol is a named set of named methods and their signatures: (defprotocol AProtocolName ;...

Added by klauern
0 Notes
No notes for extend-type