Clojure multimethods vs. protocols

后端 未结 4 1755
北荒
北荒 2020-12-02 17:11

I\'m a Clojure novice and was looking for some concrete examples of when to use protocols and when to use multimethods. I know that protocols are generally geared towards cr

4条回答
  •  时光取名叫无心
    2020-12-02 17:36

    Multimethods are more powerful and more expensive,

    use protocols when they are sufficient but if you need to dispatch based on the phase of the moon as seen from mars then multimethods are your best choice.

    Protocols exist to allow the simple stuff to stay simple and provide a way for clojure to generate very much the same bytecode that the equivalent java would. It seems that most people use protocols most of the time. I use multimethods when I need to dispatch on more than one argument, though I have to admit that this has only come up once, and full isa hierarchies are used even less often (by me). so in short use Multimethods when you need them

    the best example In my expierence is right at the start, in core.clj

提交回复
热议问题