如何动态查找Clojure函数的元数据? [英] How do I dynamically find metadata for a Clojure function?
本文介绍了如何动态查找Clojure函数的元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有以下代码:
(defn ^ {:graph-titleFunction 1} func-1
[x]
(do-something-with x))
(defn get-graph-title
[func]
(b)我希望这将返回Function 1,但它返回nil。我认为这源于以下差异,我不完全理解:
(meta func-1)
= > {:ns some-ns-info,:name func-1}
(meta#'func-1)
=> {:ns some-ns-info,:name func-1, graph-titleFunction 1}
有人可以向我解释一下吗?
因此,要获取图形标题,您需要从var的元中获取条目
:graph-title
(defmacro get-graph-title
[func]
(:graph-title(meta(var〜func))))
(get-graph-title func-1)
=> Function 1
Say I have the following code:
(defn ^{:graph-title "Function 1"} func-1 [x] (do-something-with x)) (defn get-graph-title [func] (str ((meta func) :graph-title)))I expect this to return "Function 1", but it returns nil. I think this stems from the following difference, which I don't totally comprehend:
(meta func-1) => {:ns some-ns-info, :name func-1} (meta #'func-1) => {:ns some-ns-info, :name func-1, :graph-title "Function 1"}Can someone explain this to me?
解决方案The metadata is attached to the var, not to the function.
Thus, to get the graph title, you have to get the entry
:graph-title
from the meta of the var. How do you like your macros ?(defmacro get-graph-title [func] `(:graph-title (meta (var ~func)))) (get-graph-title func-1) => "Function 1"
这篇关于如何动态查找Clojure函数的元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文