Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending protocols for custom Java classes, maps and sequences in Clojure

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

like image 710
mikera Avatar asked Oct 14 '22 02:10

mikera


1 Answers

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

like image 88
Michał Marczyk Avatar answered Oct 16 '22 16:10

Michał Marczyk