【发布时间】:2012-07-04 20:53:44
【问题描述】:
假设我定义了一个包含一些类型提示的记录,如下所示:
(defrecord person [name sex ^Integer age city])
有什么方法可以在运行时使用person 类或person 的实例来确定指定了哪些类型提示?目的是改变使用的gui组件,取决于字段的类型(注意字段的值可能是nil,所以我不能使用值的类型来确定字段的类型)。
我尝试了一些显而易见的事情,但没有成功:
; no metadata on the class, an instance, or the keys or vals of an instance
=> (meta person)
nil
=> (meta (person. "Geoff" "male" 30 "Moon base"))
nil
=> (map meta (keys (person. "Geoff" "male" 30 "Moon base")))
(nil nil nil nil)
=> (map meta (vals (person. "Geoff" "male" 30 "Moon base")))
(nil nil nil nil)
; the field is of type Object
=> (filter (fn [x] (= "age" (.getName x))) (.getFields person))
(#<Field public final java.lang.Object matt.clarity.scratch.person.age>)
; no metadata on the fields of the class
=> (map meta (.getFields person))
(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil)
【问题讨论】: