Define a protocol as a superset of another

545 views
Skip to first unread message

Alexandre Patry

unread,
Aug 28, 2011, 2:19:46 PM8/28/11
to clo...@googlegroups.com
Hi,

I would like to define a protocol as a super set of another. For example:

(defprotocol P1
  (f [this]))

(defprotocol P1WithExtras
  ;; something like :extends P1
  (g [this]))

;; P1WithExtras should now contain f and g.

I may have missed, but I did not find how to do it from the documentation.

Any suggestions on how to define a protocol as a superset of another? Or on an alternative solution?

Thanks,

Alexandre

Luc Prefontaine

unread,
Aug 28, 2011, 2:27:52 PM8/28/11
to clo...@googlegroups.com
On Sun, 28 Aug 2011 14:19:46 -0400
Alexandre Patry <patr...@iro.umontreal.ca> wrote:
Some reading:

http://david-mcneil.com/post/1475458103/implementation-inheritance-in-clojure

Luc P.

--
Luc P.

================
The rabid Muppet

Alexandre Patry

unread,
Aug 28, 2011, 2:42:46 PM8/28/11
to clo...@googlegroups.com

On 2011-08-28, at 2:27 PM, Luc Prefontaine wrote:

> On Sun, 28 Aug 2011 14:19:46 -0400
> Alexandre Patry <patr...@iro.umontreal.ca> wrote:
> Some reading:
>
> http://david-mcneil.com/post/1475458103/implementation-inheritance-in-clojure

Thanks for the quick answer.

From what I understand, this article talks about inheritance of implementation. I would like inheritance of "specification", like :

(defprotocol State
(precede? [this other-state])
(completed? [this]))


(defprotocol Hypothesis
;; Here, I would like to specify that an hypothesis must implement the State protocol

;; but also the value and successors functions
(successors [this])
(value [this]))

It would be simple interface inheritance in java, but I do not know how to express it in clojure.


>
> Luc P.
>
>> Hi,
>>
>> I would like to define a protocol as a super set of another. For
>> example:
>>
>> (defprotocol P1
>> (f [this]))
>>
>> (defprotocol P1WithExtras
>> ;; something like :extends P1
>> (g [this]))
>>
>> ;; P1WithExtras should now contain f and g.
>>
>> I may have missed, but I did not find how to do it from the
>> documentation.
>>
>> Any suggestions on how to define a protocol as a superset of another?
>> Or on an alternative solution?
>>
>> Thanks,
>>
>> Alexandre
>>
>
>
>
> --
> Luc P.
>
> ================
> The rabid Muppet
>

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Luc Prefontaine

unread,
Aug 28, 2011, 2:52:44 PM8/28/11
to clo...@googlegroups.com
I'll be more specific :)

"If you are used to Java style type inheritance you might be surprised that there is no explicit record/protocol mechanism for defining one type as a “sub-type” of another and inheriting the super-type’s implementation. You might even think that Clojure datatypes are less powerful than Java clasess… but you would be wrong."

So to summarize, there's no protocols/defrecord inheritance. The article provides an example
how to you can override implementations of an existing protocol (extend TrainedDog (merge base-behavior ...)
from another protocol to change them. It's not inheritance, it's overriding implementations.

Luc P.

On Sun, 28 Aug 2011 14:42:46 -0400

Stuart Sierra

unread,
Aug 28, 2011, 3:43:47 PM8/28/11
to clo...@googlegroups.com
Hi Alexandre,

Clojure Protocols do not support inheritance. This was a design choice. Protocols are more than just Java interfaces because they can contain method implementations for pre-existing classes. Inheritance would make the call site caching in the implementation of protocols more complicated.

You can achieve the same thing by simply extending your types to both protocols.

-Stuart Sierra
clojure.com

Alexandre Patry

unread,
Aug 28, 2011, 7:46:38 PM8/28/11
to clo...@googlegroups.com
Thanks Luc and Stuart for your answers.

I will try to make my protocols more orthogonal than hierarchical in order to make them fit this philosophy.


Reply all
Reply to author
Forward
0 new messages