3

Using extend-protocol, a protocol P can provide a default implementation for anything that implements interface I. This essentially teaches I's to do new things. If we want a type or record to provide the functionality of I we still need to extend them with I.

Is there a way to specify I behavior (it's methods) in terms of P's behavior?

What am I actually trying to accomplish

My protocol P (which is like a stream) has (seq [this] [this timeout-value]) to provide sequence access. The second arity is to return a special value if the stream expired. (Ending the sequence there would be confusing.)

P also has (close [this]).

I would like objects that extend P to be usable in clojure.core/seq (being a Sequable) and also implement java.io.Closeable. One way to accomplish this is to remove those methods from P and just implement Sequable & Closeable within the type/record. But then when somebody hands me a P can't be sure if it can be closed or seqed. Call me object oriented, but P extends I.

Looking for

  • It's not possible (for now).
  • It can be done with this code ...
  • It can be redesigned to achieve a similar effect ...

If P's seq being multiple arity is an issue, seq & seq' would do as well.

muhuk
  • 15,777
  • 9
  • 59
  • 98
  • After giving this further thought, I realize trying to get interfaces and protocols to play the way I want them to be is unreasonable. Protocols are not Clojure equivalents of interfaces. They (apparently) don't support any way of inheritance. Also you can have an object implement protocols partially (or perhaps even leave all methods unimplemented) and it compiles. I am voting to close this question and encourage anyone with rights to do so. Not deleting it because it can perhaps save someone else going through the same thought process. – muhuk Jan 18 '16 at 11:53
  • You could maybe post your comment as an answer, in case someone comes with something, or the language evolves or... – nha Jan 18 '16 at 13:09
  • @nha it's not much of an answer but an admission that the question is based on wrong expectations/perceptions. Best if it's closed, but with some commentary on why it's nonsense. – muhuk Jan 18 '16 at 14:55

0 Answers0