qiu*_*fei 8 clojure primitive-types multimethod
我希望我的程序在原始类型和它们的包装类之间采取不同的行为,例如:
(defmulti try-type class)
(defmethod try-type Integer [arg]
(println "Integer"))
(defmethod try-type Integer/TYPE [arg]
(println "int"))
Run Code Online (Sandbox Code Playgroud)
但它不起作用,虽然我尝试Integer和int
user=> (try-type (.intValue (int 2)))
Integer
nil
user=> (try-type (int 2))
Integer
nil
Run Code Online (Sandbox Code Playgroud)
那么,是否可以在原始类型上调度多方法?
======编辑======
我正在将一个谷歌番石榴包裹成clojure.其中有一个原始库,如Booleans,Dobules,Ints等.它们有一些共同的方法,所以我想尝试多方法.
不,目前不可能。函数(例如多方法调度函数)的 arg 可以是一个对象(因此基元将被装箱)或一个基元 long/double(因此对象将被拆箱)。您的场景需要一个可以接受其中任何一个并在函数内部保留这种区别的函数。
也就是说,无论您想要解决的实际问题是什么,都可能有解决方案。