defmethod

0 views
Skip to first unread message

Timothy Pratley

unread,
Jul 27, 2009, 9:48:36 AM7/27/09
to Clojure
I picked up a tip from a post from Meikel about specifying the
function name for multi-methods for debugging purposes. It got me
thinking, wouldn't this make sense as default behaviour? I guess it
looses some flexibility to have the name automatically defined but
gains some conciseness:

(defmacro defmethod
"Creates and installs a new method of multimethod associated with
dispatch-value. "
[multifn dispatch-val & fn-tail]
`(. ~multifn addMethod ~dispatch-val
(fn ~(symbol (str multifn "-" dispatch-val)) ~@fn-tail)))

; just added ~(symbol (str multifn "-" dispatch-val)) after fn
; to make it named at creation so it will show in stack traces
; but now fn-tail cannot have a custom name
; (if you ever wanted one - I can't think why)
Reply all
Reply to author
Forward
0 new messages