How to set and get multimethod metadata in clojure?

孤街浪徒 提交于 2019-12-01 01:39:42

There's no such possibility. The first reason (straightforward one) is that defmethod doesn't provide an ability to set metadata for a particular method (only defmulti allows that, but only for the whole multimethod). Second reason is that multimethod is essentially a single function, just with multiple "variants" of execution, each of which fires depending on passed parameters. Rougly speaking, from caller point of view, there's no particular difference between functions f1 and f2 defined below:

(defmulti f1 (fn [x] x))

(defmethod f1 :foo [x]
  ...)

(defmethod f1 :bar [x]
  ...)

(defmethod f1 :baz [x]
  ...)

(defn f2 [x]
  (case x
    :foo ...
    :bar ...
    :baz ...))

Personally, I'd consider depending on whether particular function is multimethod or ordinary function as relying on implementation details. Also if you need to explicitly document each method of multimehod, you should consider replacing each method with ordinary function and don't use multimethods at all.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!