有没有办法在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.
Run Code Online (Sandbox Code Playgroud)
倒数第二行不起作用,因为你无法调用var列表(identity foo),最后一行甚至都没有编译,因为编译器抱怨无法解析f.
大多数对于这个问题我已经找到了解决方案的依赖,你可以访问符号函数的定义,或者类似的东西,这样就可以做这样的事情的想法(resolve 'f)或(var f).但我想要一些我可以用于函数参数的东西,你不知道那些信息.
基本上,我想要一个表达式,我可以代替下面的问号来获取元数据#'map:
(let [x map] (??? x))
Run Code Online (Sandbox Code Playgroud)
它虽然可能是一口一口:
(let [x map]
(:doc (meta (second (first (filter #(and (var? (second %))
(= x (var-get (second %))))
(ns-map *ns*)))))))
Run Code Online (Sandbox Code Playgroud)
产生预期的结果:
"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."
Run Code Online (Sandbox Code Playgroud)
引擎盖名称空间本质上是名称到vars的映射,而vars包含函数.您可以搜索这些变量的内容以查找与您正在寻找的函数匹配的变量,然后查看它的相关变量并从该变量中获取元数据.
| 归档时间: |
|
| 查看次数: |
749 次 |
| 最近记录: |