我怎样才能得到一个多方法的var?
How can I get the var of a multimethod?
我正在尝试使用 dire 向多方法添加挂钩。作者说这可能行不通。
这是一个具有正常功能的示例:
(ns mydire.prehook
(:require [dire.core :refer [with-pre-hook!]]))
(defn times [a b]
(* a b))
(with-pre-hook! #'times
"An optional docstring."
(fn [a b] (println "Logging something interesting.")))
(times 21 2) ; => "Logging something interesting."
如您所见,with-pre-hook!
传递给 (var times)
(与 #'times
相同)。
问题是,当为多方法调用 var
时出现异常:
clojure.lang.PersistentList cannot be cast to clojure.lang.Symbol
有没有办法让它工作?
下面是我的代码示例:
(defmulti get-url identity)
(defmethod get-url :Whosebug
[site]
"http://whosebug.com")
(with-pre-hook! (var (get-method get-url :Whosebug))
(fn [x] (println "getting url for Whosebug.")))
var
是一个宏,它不评估其参数。如果你给它一个列表,它不会评估列表,它会拒绝它,因为它是一个列表而不是一个符号。
没有要附加到特定方法的 var,因为 defmethod
不会创建 var,它会修改它所附加的 multimethod 的调度。 get-method
返回的值是一个函数,而不是一个变量。
查看 dire
后,它特别需要一个 var 来执行操作,并且如果不进行一定程度的重新设计,它将无法在 multimethod 的特定方法上运行。所以不,你不能在特定方法上使用 with-pre-hook
,尽管它可能适用于多方法本身(包括它的所有方法)。
我正在尝试使用 dire 向多方法添加挂钩。作者说这可能行不通。 这是一个具有正常功能的示例:
(ns mydire.prehook
(:require [dire.core :refer [with-pre-hook!]]))
(defn times [a b]
(* a b))
(with-pre-hook! #'times
"An optional docstring."
(fn [a b] (println "Logging something interesting.")))
(times 21 2) ; => "Logging something interesting."
如您所见,with-pre-hook!
传递给 (var times)
(与 #'times
相同)。
问题是,当为多方法调用 var
时出现异常:
clojure.lang.PersistentList cannot be cast to clojure.lang.Symbol
有没有办法让它工作?
下面是我的代码示例:
(defmulti get-url identity)
(defmethod get-url :Whosebug
[site]
"http://whosebug.com")
(with-pre-hook! (var (get-method get-url :Whosebug))
(fn [x] (println "getting url for Whosebug.")))
var
是一个宏,它不评估其参数。如果你给它一个列表,它不会评估列表,它会拒绝它,因为它是一个列表而不是一个符号。
没有要附加到特定方法的 var,因为 defmethod
不会创建 var,它会修改它所附加的 multimethod 的调度。 get-method
返回的值是一个函数,而不是一个变量。
查看 dire
后,它特别需要一个 var 来执行操作,并且如果不进行一定程度的重新设计,它将无法在 multimethod 的特定方法上运行。所以不,你不能在特定方法上使用 with-pre-hook
,尽管它可能适用于多方法本身(包括它的所有方法)。