clojure - How can I get the var of a multimethod? -
i'm trying use dire add hooks multimethods. author says might not work. here example normal function:
(ns mydire.prehook (:require [dire.core :refer [with-pre-hook!]])) (defn times [a b] (* b)) (with-pre-hook! #'times "an optional docstring." (fn [a b] (println "logging interesting."))) (times 21 2) ; => "logging interesting."
as can see, with-pre-hook!
passed (var times)
(which same #'times
).
the problem when calling var
multimethod i'm getting exception: clojure.lang.persistentlist cannot cast clojure.lang.symbol
is there way make work?
below code sample:
(defmulti get-url identity) (defmethod get-url :stackoverflow [site] "http://stackoverflow.com") (with-pre-hook! (var (get-method get-url :stackoverflow)) (fn [x] (println "getting url stackoverflow.")))
var
macro, not evaluate argument. if give list, not evaluate list, reject it, because it's list , not symbol.
there no var attach specific method, because defmethod
not create var, modifies dispatch of multimethod attached to. value returned get-method
function, not var.
having looked @ dire
, needs var act on, , won't work on specific method of multimethod without amount of redesign. no, can't use with-pre-hook
on specific method, though might work on multimethod (including of methods).
Comments
Post a Comment