Gra
Gra

Reputation: 1594

Does reader/read-string attach metadata to the forms

I've read somewhere that cljs.reader/read-string attaches metadata to the forms that it creates, like the position in the string read.
Is it true? Is it documented somewhere? Thanks.

Upvotes: 2

Views: 133

Answers (2)

metasoarous
metasoarous

Reputation: 2943

I don't know about cljs.reader, but if you use clojure.tools.reader, you can do this. It's not particularly well documented, but you can see how by looking at the tests: https://github.com/clojure/tools.reader/blob/master/src/test/cljs/cljs/tools/metadata_test.cljs#L62-L70

In short, you have to pass the string to clojure.tools.reader.reader-types/indexing-push-back-reader, and from there to clojure.tools.reader/read. (In the test/example above, they first pass to reader-types/string-push-back-reader, but this doesn't appear to be strictly necessary).

Upvotes: 0

Taylor Wood
Taylor Wood

Reputation: 16194

read-string doesn't add metadata to the returned form:

=> (meta (cljs.reader/read-string "(prn 0)"))
nil

Your compiled functions/defs/vars will have this type of metadata though:

=> (meta #'my-fn)
{:ns app.core,
 :name my-fn,
 :file "src/cljs/app/core.cljs",
 :end-column 20,
 :column 1,
 :line 125,
 :end-line 125,
 :arglists ([{:keys [x]}]),
 :doc nil,
 :test nil}

Upvotes: 1

Related Questions