【发布时间】:2012-07-28 16:47:20
【问题描述】:
我希望我的程序在原始类型和它们的包装类之间表现不同,例如:
(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
那么,是否可以在原始类型上调度多方法?
======编辑======
我正在将 google guava 包装到 clojure 中。里面有一个原始库,比如Booleans、Dobules、Ints等。它们有一些共同的方法,所以我想试试multimethod。
【问题讨论】:
-
您能解释一下您为什么要这样做吗?目前不可能对原语进行调度,但可能有一种实现相同目标的好方法(谷歌“XY问题”)
标签: clojure primitive-types multimethod