Clojure multimethods vs. protocols

后端 未结 4 1762
北荒
北荒 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:42

    As mention by Arthur, multimethods are more powerful and more expensive. Indeed, protocols can be thought of as a special case of mutlimethods where the dispatch function is class. Of course, this is not really the case as protocols are more than that.

    If you need to dispatch on something other than the class of the first argument, you'll need to use a multimethod, or redesign. Dispatching on type is a good use case for protocols.

提交回复
热议问题