On Oct 28, 12:46 pm, Howard Lewis Ship <hls...@gmail.com> wrote:
> (extend-type Asset
>   ToAttributeValueString
>   (to-attribute-value-string [asset] (:client-url asset)))

As Alex mentioned we have an "adapt-protocol" macro [1] [2] that I
think will do what you want. It would be invoked as:

(adapt-protocol Asset
  ToAttributeValueString
  (to-attribute-value-string [asset] (client-url asset)))

>From your original code, I changed "extend-type" to "adapt-protocol"
and I fixed what I think is a type by changing ":client-url" to
"client-url".

There are different ways to think of what this does. One way is to
think of adapting one protocol to another. The adapt-protocol call
defines how to adapt an Asset object to an ToAttributeValueString
object.

Another way is to think of layering the protocols. The
ToAttributeValueString protocol is layered on top of the Asset
protocol by defining how to satisfy the ToAttributeValueString
protocol in terms of the Asset protocol.

At a lower level what this adapt-protocol call means is: for any type
that implements Asset but does not already implement
ToAttributeValueString, add an implementation of Asset to that type
using the provided functions. This implementation will be added the
first time that an attempt is made to access the type via the
ToAttributeValueString protocol. It is useful to be able to define
this in one place rather than tracking down all of the types that
implement the Asset protocol and extending them to support the
ToAttributeValueString protocol.

I don't think it is useful to try and think of this as interface
inheritance because it is not the same as interface inheritance in
Java.

We are using this code in production to do exactly what you described.
The only real issue to be aware of is that once an adapter is
“installed” for a class subsequent calls to adapt-protocol do not
affect the class. This can cause pain at dev time when you are
changing the adapter code and want to dynamically redefine an adapter.
I talk about this issue and a way to address it at dev time in the
blog post [1].

-David

[1] http://david-mcneil.com/post/3495351254/clojure-protocol-adapters
[2] https://github.com/david-mcneil/clojure-adapt

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to