I may have missed the whole point about protocols but my question is, can protocols be used to dictate how to iterate a custom data structure or how println would print the object?
Assuming a map with two vectors,
{:a [] :b []}
When called first on it I would like to take from the :a vector but when conj on this structure i would like to conj to :b. Can I use protocols to achieve this type of behavior?
Some things are still implemented as Java interfaces in Clojure; of those, I’d say some are likely to stay that way forever to ease cooperating with Clojure code from other JVM languages.
Fortunately, when defining a type using
deftype, you can have the new type implement any Java interfaces you require (which Brian mentioned in a comment above), as well as any methods ofjava.lang.Object. An example to match your description might look like this:A sample of what you can do with it at the REPL:
Note that the REPL prints it as a seq; I believe that’s because of the inline implementation of
clojure.lang.ISeq. You could skip it and replace theseqmethod with one returning(seq a)for a printed representation using the customtoString.stralways usestoString, though.If you need custom behaviour of
prfamily functions (includingprintlnetc.), you’ll have to look into implementing a customprint-methodfor your type.print-methodis a multimethod defined inclojure.core; have a look at core_print.clj in Clojure’s sources for example implementations.