5

I have created a protocol in Clojure 1.2 that handles my own Java classes and has default handling for a generic java.lang.Object. The code looks something like:

(extend-protocol PMyProtocol
  my.java.ClassName
    (protocol-function [c]
      "My Java class result")

  java.lang.Object
    (protocol-function [c]
      "Default object result"))

How should I extend this to have special handling for the standard Clojure data structures (in particular maps, vectors and sequences)?

1 Answer 1

4

All of Clojure's persistent data structures implement interfaces which extend clojure.lang.PersistentCollection. Clojure's transient collections implement clojure.lang.TransientCollection. You can extend your protocol to these as if you were extending it to a class (though dealing with just the persistent collections might make more sense).

Sign up to request clarification or add additional context in comments.

2 Comments

To name them explicitly in case different handling is necessary: c.l.IPersistentMap, c.l.IPersistentVector, c.l.IPersistentSet (there is some system in there...) and for sequences c.l.ISeq.
Thanks Michal and kotarak! I ended up using clojure.lang.IPersistentMap and clojure.lang.ISeq which are working well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.