with-meta usage

36 views
Skip to first unread message

Apurva

unread,
Aug 31, 2008, 6:58:16 AM8/31/08
to Clojure
Hi Clojure experts,

I am newbie to Clojure and was exploring metadata.

The following didn't work:

user=> (def v 10)
#'user/v
user=> (with-meta v {:info 1})
java.lang.IncompatibleClassChangeError
java.lang.IncompatibleClassChangeError
at clojure.with_meta__47.invoke(boot.clj:159)
at user.eval__2228.invoke(Unknown Source)
at clojure.lang.Compiler.eval(Compiler.java:3847)
at clojure.lang.Repl.main(Repl.java:75)


While the following worked:

user=> (def vo [1 2 3])
#'user/vo
user=> (with-meta vo {:info 1})
[1 2 3]
user=> (meta (with-meta vo {:info 1}))
{:info 1}

I was wondering why the first scenario didn't work but couldn't find
details on this. Can someone please explain?

Regards,
Apurva

Meikel Brandmeyer

unread,
Aug 31, 2008, 11:16:01 AM8/31/08
to clo...@googlegroups.com
Hello,

> I was wondering why the first scenario didn't work but couldn't find
> details on this. Can someone please explain?

I also stumbled over this issue. Please read this sentence from the
"Metadata" section on clojure.org (http://clojure.org/metadata)

> "Symbols and collections support metadata"

I also missed this. It's pretty subtle. Since 10 is neither a Symbol nor
a Collection. You get the error. You can set the metadata of the Var:

---8<---
user=> (def #^{:Some :MetaData} v 10)
#'user/v
user=> (meta #'v)
{:Some :MetaData, :file "NO_SOURCE_FILE", :name v, :ns #<Namespace:
user>, :line 1}
---8<---

Hope this helps.

Sincerely
Meikel

Apurva Sharan

unread,
Aug 31, 2008, 11:46:27 PM8/31/08
to clo...@googlegroups.com
Thanks Meikel!

That makes it clearer...

Regards,
Apurva

Reply all
Reply to author
Forward
0 new messages