How to implement an interface via protocols?

257 Views Asked by At

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.

0

There are 0 best solutions below