views:

70

answers:

1

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

+2  A: 

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

Michał Marczyk
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`.
kotarak
Thanks Michal and kotarak! I ended up using clojure.lang.IPersistentMap and clojure.lang.ISeq which are working well.
mikera