On 09.02.2010, at 02:14, Stuart Sierra wrote:

> On Feb 8, 6:13 pm, aria42 <ari...@gmail.com> wrote:
>> (defprotocol Span
>>   (start [self])
>>   (stop [self])
>>   (span-length [self]))
>> 
>> Now I know I can just make span-length a function on Span as opposed
>> to part of the protocol. Is that what one should do?
> 
> Yes.

I would say "it depends".

I have a similar situation in my multiarray package 
(http://code.google.com/p/clj-multiarray/). In the multiarray protocol, I have 
two functions, "shape" and "rank", with the latter being by definition the same 
as (comp count shape). However, I still have "rank" in the protocol, because 
for some implementations it is more efficient to compute the rank directly, 
rather than construct a shape vector just for computing its length afterwards.

In such situations it is useful to provide a default implementation and leave 
it up to each type to implement a more efficient alternative or not. With 
extend and the maps that go with it, this is easy to achieve: make a map with 
the default implementations, and merge this with the type-specific 
implementations fed to extend.

Konrad.

-- 
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