开发者

Adding with-meta data to clojure structures

开发者 https://www.devze.com 2023-02-19 12:07 出处:网络
I want to add meta-data to different items in a map, but I get an error in Clojure if I rry this with:

I want to add meta-data to different items in a map, but I get an error in Clojure if I rry this with:

{:a 
    (with-meta 
        1
        {:some-meta-tag "some-meta开发者_运维知识库-data-value"}
    )
} 

: Is this possible?


I could be wrong, but think you can't attach metadata to a number:

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

From the docs

"Symbols and collections support metadata, a map of data about the symbol or collection."

This seemed to work:

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

And

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

精彩评论

暂无评论...
验证码 换一张
取 消