4

有没有办法在clojure中为函数的参数获取元数据?实际上,此问题中发布的答案通常不起作用:

user> (defn foo "informative dox!" [] 1)
#'user/foo
user> (defmacro get-docs [func] `(:doc (meta (var ~func))))
#'user/get-docs
user> (get-docs foo)
"informative dox!"
user> (get-docs (identity foo))
; Evaluation aborted.
user> (defn process-docs [f] (let [docs (get-docs f)] (reverse docs)))
; Evaluation aborted.

倒数第二行不起作用,因为您无法调用varlist (identity foo),最后一行甚至无法编译,因为编译器抱怨无法解析f

我发现这个问题的大多数解决方案都依赖于你可以访问函数定义中的符号或类似的东西,这样你就可以做类似的事情(resolve 'f)or (var f)。但是我想要一些我可以在函数的参数上使用的东西,你不知道这些信息。

从本质上讲,我想要一个表达式来代替下面的问号来获取元数据#'map

(let [x map] (??? x))
4

1 回答 1

5

尽管可能,但它是满口的:

(let [x map]
   (:doc (meta (second (first (filter #(and (var? (second %))
                                            (= x (var-get (second %))))
                                      (ns-map *ns*)))))))

产生所需的结果:

"Returns a lazy sequence consisting of the result of applying f to the
 set of first items of each coll, followed by applying f to the set
 of second items in each coll, until any one of the colls is\n  exhausted.  Any remaining items in other colls are ignored. Function
 f should accept number-of-colls arguments."

在引擎盖下命名空间本质上是名称到变量的映射,变量包含函数。您可以在这些 var 的内容中搜索与您正在寻找的函数匹配的内容,然后查看它的关联 var 并从该 var 中获取元数据。

于 2012-09-14T23:35:43.810 回答