Clojure 如何在基元类型上分派multimethod?

Clojure 如何在基元类型上分派multimethod?,clojure,primitive-types,multimethod,Clojure,Primitive Types,Multimethod,我希望我的程序在基本类型和它们的包装类之间的行为有所不同,例如: (defmulti try-type class) (defmethod try-type Integer [arg] (println "Integer")) (defmethod try-type Integer/TYPE [arg] (println "int")) 但它不起作用,尽管我尝试了Integer和int user=> (try-type (.intValue (int 2))) Integer

我希望我的程序在基本类型和它们的包装类之间的行为有所不同,例如:

(defmulti try-type class)

(defmethod try-type Integer [arg]
  (println "Integer"))

(defmethod try-type Integer/TYPE [arg]
  (println "int"))
但它不起作用,尽管我尝试了Integer和int

user=> (try-type (.intValue (int 2)))
Integer
nil
user=> (try-type  (int 2))
Integer
nil
那么,有可能在基元类型上分派multimemethod吗

=======编辑======


我正在把谷歌番石榴包装成clojure。里面有一个基本的库,比如Booleans、Dobules、Ints等。它们有一些共同的方法,所以我想尝试multimethod。

不,目前不可能。函数的arg(如multi-method dispatch函数)可以是对象(因此原语将被装箱)或原语long/double(因此对象将被解除装箱)。您的场景需要一个函数,该函数可以接受其中一个,并在函数内部保留该区别


也就是说,无论您试图解决的实际问题是什么,都可能有解决方案。

您能解释一下为什么要这样做吗?目前不可能在原语上进行调度,但可能有一种很好的方法来实现相同的目标(google“XY问题”)