I have a protocol called IExample
and I define a record type A
that implements it:
(defprotocol IExample
(foo [this] "do something")
(bar [this] "do something else"))
(defrecord A [field1 field2]
IExample
(foo [this]
(+ field1 field2))
(bar [this]
(- field1 field2)))
Let's say I want to extend another (basic) type B
to implement this protocol, but I know how to convert from B
to A
:
(defn B-to-A
"converts a B object to an A object"
[Bobj] ...)
because I have this conversion, I can delegate all calls of the IExample
protocol
on a B
to the IExample
protocol on an A
by delegating them:
(extend B
IExample {
:foo (fn [this] (foo (B-to-A this)))
:bar (fn [this] (bar (B-to-A this)))})
This, however, seems as an awful lot of boilerplate (especially for bigger protocols) that is not clojure-idiomatic.
How can I tell clojure just to implicitly convert B
to A
every time
an IExample
function is called on a B
object, using the B-to-A
function?