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)?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With