(defn class2 [x y]
[(class x) (class y)])
(defmulti foo class2)
(defmethod foo [String String] [a b]
(println a b))
(defmethod foo [Long Long] [a b]
(println (+ a b)))
From the REPL:
user=> (foo "bar" "baz")
bar baz
nil
user=> (foo 1 2)
3
nil
You could also consider using type
instead of class
; type
returns :type
metadata, delegating to class
if there is none.
Also, class2
does not have to be defined at top level; passing (fn [x y] ...)
as the dispatch function to defmulti
is fine too.