Back
defprotocol (clj)
(source)macro
(defprotocol name & opts+sigs)
A protocol is a named set of named methods and their signatures:
(defprotocol AProtocolName
;optional doc string
"A doc string for AProtocol abstraction"
;options
:extend-via-metadata true
;method signatures
(bar [this a b] "bar docs")
(baz [this a] [this a b] [this a b c] "baz docs"))
No implementations are provided. Docs can be specified for the
protocol overall and for each method. The above yields a set of
polymorphic functions and a protocol object. All are
namespace-qualified by the ns enclosing the definition The resulting
functions dispatch on the type of their first argument, which is
required and corresponds to the implicit target object ('this' in
Java parlance). defprotocol is dynamic, has no special compile-time
effect, and defines no new types or classes. Implementations of
the protocol methods can be provided using extend.
When :extend-via-metadata is true, values can extend protocols by
adding metadata where keys are fully-qualified protocol function
symbols and values are function implementations. Protocol
implementations are checked first for direct definitions (defrecord,
deftype, reify), then metadata definitions, then external
extensions (extend, extend-type, extend-protocol)
defprotocol will automatically generate a corresponding interface,
with the same name as the protocol, i.e. given a protocol:
my.ns/Protocol, an interface: my.ns.Protocol. The interface will
have methods corresponding to the protocol functions, and the
protocol will automatically work with instances of the interface.
Note that you should not use this interface with deftype or
reify, as they support the protocol directly:
(defprotocol P
(foo [this])
(bar-me [this] [this y]))
(deftype Foo [a b c]
P
(foo [this] a)
(bar-me [this] b)
(bar-me [this y] (+ c y)))
(bar-me (Foo. 1 2 3) 42)
=> 45
(foo
(let [x 42]
(reify P
(foo [this] 17)
(bar-me [this] x)
(bar-me [this y] x))))
=> 17
Examples
noprompt/meander
(ns multimethods
(:refer-clojure :exclude [defmethod defmulti])
(:require
#?(:clj [clojure.core :as clj] :cljs [cljs.core :as cljs])
[meander.epsilon :as m]))
(defprotocol IMeanderMethods
(-set-fn [this f]))
puppetlabs/trapperkeeper
(ns puppetlabs.trapperkeeper.app
(:require [schema.core :as schema]
[puppetlabs.trapperkeeper.services :as s]
[clojure.core.async.impl.protocols :as async-prot])
(:import (clojure.lang IDeref)))
(defprotocol TrapperkeeperApp
"Functions available on a trapperkeeper application instance"
(get-service [this service-id] "Returns the service with the given service id")
(service-graph [this] "Returns the prismatic graph of service fns for this app")
(app-context [this] "Returns the application context for this app (an atom containing a map)")
(check-for-errors! [this] (str "Check for any errors which have occurred in "
"the bootstrap process. If any have "
"occurred, throw a `java.lang.Throwable` with "
"the contents of the error. If none have "
"occurred, return the input parameter."))
(init [this] "Initialize the services")
(start [this] "Start the services")
(stop [this] [this throw?] "Stop the services")
(restart [this] "Stop and restart the services"))
frenchy64/fully-satisfies
(ns io.github.frenchy64.fully-satisfies.shared-protocol
(:refer-clojure :exclude [defprotocol])
(:require [clojure.core :as cc]))
(defmacro def-shared-protocol
[nme & body]
`(do (cc/defprotocol ~nme ~@body)
(let [protocol-var# (var ~nme)]
(-reset-methods
(alter-var-root protocol-var#
update :method-builders
#(into {}
(map (fn [[^clojure.lang.Var v# build#]]
(let [cache# (clojure.lang.MethodImplCache. (symbol v#) @protocol-var# (keyword (.sym v#)))
^clojure.lang.AFunction f# (build# cache#)
shared-build# (fn [cache#]
(set! (.__methodImplCache f#) cache#)
f#)]
[v# shared-build#])))
%))))
'~nme))
(defmacro defprotocol [& args] `(def-shared-protocol ~@args))
aliostad/deep-learning-lang-detection
(ns dialog-play-bot-for-layer.component.token-manager
"Manage yepl access-token, dialog-play channel-id, layer conversation-id."
(:require [integrant.core :as ig]
[clojure.core.cache :as cache]))
(defprotocol ITokenManager
(new-conversation [this layer-conversation-id dialog-play-channel-id])
(get-dialog-play-channel-id [this layer-conversation-id])
(register-yelp-access-token [this yelp-access-token])
(get-yelp-access-token [this]))
polygloton/pipeline
(ns ^{:doc "Protocols that are used by the listener and worker.
Not intended for external use."}
pipeline.process.protocols
(:require [clojure.core.async :as async]
[pipeline.protocols :as prots]))
(defprotocol PipelineTaskState
"State tracked by the listener that is associated with an
input-channel that it is listening to"