Skip to content

Latest commit

 

History

History
53 lines (46 loc) · 3.11 KB

defprotocol.md

File metadata and controls

53 lines (46 loc) · 3.11 KB

defprotocol

(defprotocol Name docstring? (method [target & args]+ docstring?)*)

Declares a protocol: a method-table global plus one dispatcher per method over the target's tag (the multimethod shape without the hierarchy search). The name answers the protocol's map, in the oracle's shape -- the interface under :on/:on-interface (a class keyword here), each method's parameter vectors under :sigs, the method map and the extension table under :impls -- which extend, satisfies?, extends? and extenders accept like the name at a call site. :var and :method-builders are not carried. The interface's dotted name (user.P, my_app.core.P) is its class value: it prints as the name, answers class?, and instance? of it, isa? and a multimethod dispatching on class take a record, deftype or reify whose body implements the protocol (an extension does not), like the oracle's -- a print-method of it too; it is not = to the :on-interface keyword. A call dispatches on the target's exact tag, then on a class the target extends or implements that the protocol was extended to (a throwable, IRef, ...: extend-protocol), then the Object row; a miss with no Object row signals, like the oracle. A method declares one parameter vector per arity, each taking the target first, and a call reaches the implementation's arity of its argument count. A defrecord, deftype or reify body implements another arity by naming the method again over another parameter vector, each one an arity the protocol declares; extend-protocol, extend-type and extend spell the arities as the clauses of a fn. A count the implementation does not take signals an ArityException (the oracle throws an AbstractMethodError for an arity an inline body leaves out).

A protocol may declare no methods (a marker protocol): extend-type, extend-protocol, extend, defrecord, deftype and reify name it with no implementations, and satisfies?/extends? answer as for any other protocol.

With :extend-via-metadata true a value also implements a method through its metadata (with-meta), keyed by the namespace-qualified method symbol (`area or 'user/area). The order is the oracle's: an implementation in a defrecord/deftype/reify body, then the metadata, then the extend-type rows and Object. satisfies? does not look at metadata, like the oracle.

(defprotocol P (greet [x]))
(defrecord R [name] P (greet [_] name))
(extend-protocol P nil (greet [_] :nobody))
(println (greet (->R "Ada"))) ; Ada
(println (greet nil))         ; :nobody

(defprotocol Area :extend-via-metadata true (area [s]))
(println (area (with-meta {:w 2 :h 3} {`area (fn [s] (* (:w s) (:h s)))}))) ; 6

(defprotocol Scaled (scale [s] [s k]))
(defrecord Square [side] Scaled (scale [_] (* side side)) (scale [this k] (* k (scale this))))
(extend-protocol Scaled String (scale ([s] (count s)) ([s k] (* k (count s)))))
(println (scale (->Square 3)) (scale (->Square 3) 2)) ; 9 18
(println (scale "abc") (scale "abc" 2))               ; 3 6