Differences in read-string with quoted forms

254 views
Skip to first unread message

mbosse...@netflix.com

unread,
Aug 17, 2017, 5:21:59 PM8/17/17
to ClojureScript
I'm new to ClojureScript, but I didn't find this difference from Clojure noted anywhere. Is this expected?

In Clojure (expected):

(clojure.core/read-string "'(foo 42)")
=> (quote (foo 42))

In ClojureScript:

(cljs.reader/read-string "'(foo 42)")
=> '

If I do the long form quote it works fine:

(cljs.reader/read-string "(quote (foo 42))")
=> (quote (foo 42))

But it appears that it's just reading the quote as a symbol in cljs:

(type (cljs.reader/read-string "'(foo 42)"))
=> cljs.core/Symbol

Thanks,
Matt

Avi Avicenna

unread,
Aug 19, 2017, 3:19:18 AM8/19/17
to ClojureScript
clojurescript's cljs.reader/read-string expects the input string in edn format

(doc cljs.reader/read-string)
=>
-------------------------
cljs.reader/read-string
([s] [opts s])
  Reads one object from the string s.
   Returns nil when s is nil or empty.

   Reads data in the edn format (subset of Clojure data):

   opts is a map as per cljs.tools.reader.edn/read


the cljs.reader/read-string equivalent in clojure is in clojure.edn namespace. 
clojure.edn/read-string returns the same output

(clojure.edn/read-string "'(foo 42)")
=> '

(cljs.reader/read-string "'(foo 42)")
=> '

the top level structure beginning with ' is not specified in edn format, so maybe that's why the result is kinda weird.

Yours,
Avi

Dustin Getz

unread,
Aug 19, 2017, 9:33:01 AM8/19/17
to clojur...@googlegroups.com
what is the right way to read an entire string with many cljs forms (including maybe a ns form) at runtime?

mbosse...@netflix.com

unread,
Aug 21, 2017, 12:19:22 PM8/21/17
to ClojureScript
Ahh - that makes sense now. Still a little weird, but consistent I guess

Thanks!

Alan Moore

unread,
Aug 22, 2017, 12:45:04 PM8/22/17
to ClojureScript
Dustin,

I think the biggest restriction is the lack of the special form eval in the ClojureScript runtime (unless you are using a self hosted version?) so anything that could result in reading such a form is not supported.

Alan

Ivan Pierre

unread,
Aug 23, 2017, 1:18:26 PM8/23/17
to ClojureScript
It's clear that edn reader will not apply macro substitutions. But this is made in eval function.
Character macros are in the reader (){}[], these are managed by edn as a standard. But ' (quote) is the injection of a special form that has a functional meaning in the evaluation, no0t on the reading. So edn reader doesn't have it, as we wish to have data, not code.
I think it's a bad call on the cljs version of read-string that read edn and not s-expressions... to be seen in code. I think the solution will be obvious.
You also get some weird problems with backquote and splice on Clojure, when you make some nesting you get some exploding representations caused by the way toString() don't use a real inverse function of read for `, ~, and ~@, not tested on ClojureScript. ;) 

Alan Moore

unread,
Aug 23, 2017, 1:39:23 PM8/23/17
to clojur...@googlegroups.com
Ivan,

Thanks for the detailed explanation. Clearly I was off the mark re eval. I guess it was my knee-jerk reaction from my (different) frustrations with the lack of eval. My bad.

Alan
--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/R6waOGeuxBU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at https://groups.google.com/group/clojurescript.

Ivan Pierre

unread,
Aug 25, 2017, 5:37:22 PM8/25/17
to clojur...@googlegroups.com
You are right to ask yourself. Think about passing real code and not EDN data to another process, if you switch the readers you can have bad surprises, some have passed some time on this very problem ;)

To unsubscribe from this group and all its topics, send an email to clojurescript+unsubscribe@googlegroups.com.

To post to this group, send email to clojur...@googlegroups.com.
Visit this group at https://groups.google.com/group/clojurescript.

--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/R6waOGeuxBU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescript+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages