将-meta数据添加到clojure结构中

时间:2011-03-23 18:08:15

标签: clojure

我想将元数据添加到地图中的不同项目中,但如果我用以下方法,我会在Clojure中收到错误:

{:a 
    (with-meta 
        1
        {:some-meta-tag "some-meta-data-value"}
    )
} 

:这可能吗?

1 个答案:

答案 0 :(得分:8)

我可能错了,但认为您无法将元数据附加到数字:

user=> (with-meta 1 {:meta-tag "foo"})
java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IObj

来自docs

“符号和集合支持元数据,有关符号或集合的数据映射。”

这似乎有效:

user=> {:a (with-meta 'foo {:meta-tag "foo"})}
{:a foo}

user=> (meta (:a {:a (with-meta 'foo {:meta-tag "foo"})}))
{:meta-tag "foo"}