defmethod抓住所有

Bri*_*ham 8 clojure multimethod

我有一个专门研究两个参数的多方法:

(defmulti get-tag-type (fn [type tag] [type tag]))
Run Code Online (Sandbox Code Playgroud)

拥有该类型允许我将不同的defmethod调用分组:

(defmethod get-tag-type [::cat 0] [type tag] ::tiger)
(defmethod get-tag-type [::cat 1] [type tag] ::lion)
(defmethod get-tag-type [::cat 2] [type tag] ::jaguar)

(defmethod get-tag-type [::dog 0] [type tag] ::poodle)
(defmethod get-tag-type [::dog 1] [type tag] ::australian-shepherd)
(defmethod get-tag-type [::dog 2] [type tag] ::labrador-retriever)
Run Code Online (Sandbox Code Playgroud)

但是,有时候,我想要一个catch all或默认为其中一个组,如果没有其他组匹配则会调用它们:

(defmethod get-tag-type [::dog :default] ::mutt)
Run Code Online (Sandbox Code Playgroud)

但是,除非tag实际上这不起作用:default.

有什么好办法来实现这个目标?

Ale*_*art 8

:default如果没有其他方法匹配,则多方法支持使用(可配置)值标识的回退方法.

在您的情况下,您只需添加:

(defmethod get-tag-type :default [type tag]
  (do special behaviour here ...))
Run Code Online (Sandbox Code Playgroud)

举个例子,值得注意的是你可以使用Clojure关键字建立层次结构,而multimethod dispatch可以理解这些层次结构:http://clojure.org/multimethods


use*_*464 7

您的调度函数需要知道已定义了哪些映射,以便它可以决定何时采用默认值.该methods函数将返回这些映射给您.

(defmulti get-tag-type (fn [type tag] 
                         (let [mlist (methods get-tag-type)]
                           (if-let [d (get mlist [type tag])] 
                             [type tag]
                             [type :default]))))
Run Code Online (Sandbox Code Playgroud)