I usually prefer protocols : they offer better performance and do a great job with the general case (separate sending by type).
Sometimes you really need a more complicated submission, in which case you can:
- Go through the whole pig and use multimethods
- Write a short piece of custom code (usually
(cond .....) ) to handle sending. This is sometimes better than multimethods because, for example, multimethods do not handle value ranges very well.
A useful additional tip is to write your main (publicly exposed) functions in such a way as to delegate the corresponding protocol function.
eg. it could be something like:
(defn my-api-function [ab] "Do interesting things with a and b" (multimethod-function (protocol-function a) (protocol-function b)))
This gives you great flexibility to further modify your internal implementation without requiring extensive refactoring or affecting the calling code.
mikera
source share