You're viewing version 1.2.0 of extend-type. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

extend-type

clojure.core

  • (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 top

  • ;;; 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'.
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core_deftype.clj:698 top

(defmacro extend-type 
  "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] ...)})"
  {:added "1.2"} 
  [t & specs]
  (emit-extend-type t specs))
Vars in clojure.core/extend-type: defmacro
Used in 0 other vars

Comments top

No comments for extend-type. Log in to add a comment.