Adding meta data to clojure structures

I want to add metadata to various elements on the map, but I get an error message in Clojure if I set this with:

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

: Is it possible?

+4
source share
1 answer

I could be wrong, but I think that you cannot 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 docs

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

It 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"} 
+7
source

Source: https://habr.com/ru/post/1344967/


All Articles