我想将元数据添加到地图中的不同项目中,但是如果我使用以下方法,我会在Clojure中收到错误:
{:a
(with-meta
1
{:some-meta-tag "some-meta-data-value"}
)
}
Run Code Online (Sandbox Code Playgroud)
: 这可能吗?
我可能错了,但认为您无法将元数据附加到数字:
user=> (with-meta 1 {:meta-tag "foo"})
java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IObj
Run Code Online (Sandbox Code Playgroud)
来自文档
"符号和集合支持元数据,有关符号或集合的数据映射."
这似乎有效:
user=> {:a (with-meta 'foo {:meta-tag "foo"})}
{:a foo}
Run Code Online (Sandbox Code Playgroud)
和
user=> (meta (:a {:a (with-meta 'foo {:meta-tag "foo"})}))
{:meta-tag "foo"}
Run Code Online (Sandbox Code Playgroud)