A single Clojure macro for streamlined protocol extension.
A single Clojure macro for streamlined protocol extension.
(multi-extend-protocol protocol & types+methods)A version of clojure.core/extend-protocol with (possibly) more conciseness.
Extends protocols to multiple types which share identical implementations. A series of names appearing before an implementation of the protocol expands into repeated implementations for each individual type.
Recommended: Supply class/type names as symbols or nil; expressions intended
to be evaluated will convey the inconsistent behavior of the underlying
clojure.core/extend-protocol.
Note: No additional checking is performed to ensure that the protocol's implementation is valid.
Example:
(multi-extend-protocol AProtocol
AType
BType
AClass
(foo [x] x)
(bar [y] y)
(baz [z] z)
DType
EClass
(foo [x] (inc x))
(bar [y] (dec y))
(baz [z] (identity z))
FType
nil
(foo [_] 99)
(bar [_] 42)
(baz [_] 0))
...expands to...
(clojure.core/extend-protocol AProtocol
AType
(foo [x] x)
(bar [y] y)
(baz [z] z)
BType
(foo [x] x)
(bar [y] y)
(baz [z] z)
AClass
(foo [x] x)
(bar [y] y)
(baz [z] z)
DType
(foo [x] (inc x))
(bar [y] (dec y))
(baz [z] (identity z))
EClass
(foo [x] (inc x))
(bar [y] (dec y))
(baz [z] (identity z))
FType
(foo [_] 99)
(bar [_] 42)
(baz [_] 0)
nil
(foo [_] 99)
(bar [_] 42)
(baz [_] 0))
A version of `clojure.core/extend-protocol` with (possibly) more conciseness. Extends protocols to multiple types which share identical implementations. A series of names appearing before an implementation of the protocol expands into repeated implementations for each individual type. Recommended: Supply class/type names as symbols or `nil`; expressions intended to be evaluated will convey the inconsistent behavior of the underlying `clojure.core/extend-protocol`. Note: No additional checking is performed to ensure that the protocol's implementation is valid. Example: ```clojure (multi-extend-protocol AProtocol AType BType AClass (foo [x] x) (bar [y] y) (baz [z] z) DType EClass (foo [x] (inc x)) (bar [y] (dec y)) (baz [z] (identity z)) FType nil (foo [_] 99) (bar [_] 42) (baz [_] 0)) ``` ...expands to... ```clojure (clojure.core/extend-protocol AProtocol AType (foo [x] x) (bar [y] y) (baz [z] z) BType (foo [x] x) (bar [y] y) (baz [z] z) AClass (foo [x] x) (bar [y] y) (baz [z] z) DType (foo [x] (inc x)) (bar [y] (dec y)) (baz [z] (identity z)) EClass (foo [x] (inc x)) (bar [y] (dec y)) (baz [z] (identity z)) FType (foo [_] 99) (bar [_] 42) (baz [_] 0) nil (foo [_] 99) (bar [_] 42) (baz [_] 0)) ```
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |