【发布时间】:2010-07-25 19:22:01
【问题描述】:
我正在尝试在编译器中使用 clojure,因此需要参数化对 deftype 的调用;但是,我很难使类型提示得以贯彻。考虑以下代码:
(defn describe [x]
(let [fields (.getDeclaredFields x)
names (map #(.getName %) fields)
types (map #(.getType %) fields)]
(interleave types names)))
(defn direct [] (deftype direct-type [^int x]))
(defn indirect-helper [] (list ^int (symbol "x")))
(defn indirect [] (eval `(deftype ~(symbol "indirect-type") ~(indirect-helper))))
以及来自 REPL 的以下会话:
Clojure 1.2.0-master-SNAPSHOT
1:1 user=> #<Namespace dataclass>
1:2 dataclass=> (direct)
dataclass.direct-type
1:3 dataclass=> (indirect)
dataclass.indirect-type
1:4 dataclass=> (describe direct-type)
(int "x")
1:5 dataclass=> (describe indirect-type)
(java.lang.Object "x")
请注意,为间接类型生成的类丢失了直接类型所具有的 ^int 提示。如何让这些提示贯彻执行?
【问题讨论】: