5

This prints :bar in Clojure as I would expect:

(println (:foo (clojure.tools.reader.edn/read-string "{:foo :bar}")))
;=> :bar

But this prints nil in ClojureScript:

(println (:foo (cljs.reader/read-string "{:foo :bar}")))
;=> nil

To make things stranger, this prints :bar in ClojureScript as I would expect:

(let [data (cljs.reader/read-string "{:foo :bar}")]
  (println ((first (keys data)) data )))
;=> :bar

How do I access a value in a map that was created by the reader? Is this a character encoding thing?

Edit

Here's the namespace as requested in the comments:

(ns clojuresite.homepage
  (:require-macros [hiccups.core :as hiccups])
  (:require [hiccups.runtime :as hiccupsrt]
            [cljs.nodejs :as node]
            [cljs.reader :as reader]))
bmaddy
  • 876
  • 9
  • 16
  • When running the second example in a fresh clojurescript repl, I get the correct answer. What clojurescript version are you using? – Jared314 Sep 26 '13 at 22:53
  • The second example works for me as well, could you include the ns form for context? – Arthur Ulfeldt Sep 26 '13 at 23:06
  • Well that's really surprising to me. My cljs version is 0.0-1896. How did you get it to work in the repl? Every time I tried `(require '[cljs.reader :as reader])` I got a compile error. – bmaddy Sep 26 '13 at 23:32
  • I used the namespace form to require it in the repl. – Jared314 Sep 26 '13 at 23:35

2 Answers2

2

the return value of println is nil and perhaps the output from the print is likely going into another buffer if your are using emacs or being logged somewhere else. Perhaps you are seeing the return value of println instead of it's output.

ClojureScript:cljs.user> (ns cljs.user (:require [cljs.reader :as edn]))
nil
ClojureScript:cljs.user> (println (:foo (cljs.reader/read-string "{:foo :bar}")))
:bar
nil

vs:

ClojureScript:cljs.user> (:foo (cljs.reader/read-string "{:foo :bar}"))
:bar
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
2

Try this:

(.log js/console
  ((keyword "foo") (cljs.reader/read-string "{:foo :bar}")))

If that works, and generates ﷐:bar, you have old generated code hanging around and you should run a lein cljsbuild clean.

There was a change in 0.0-1877 that switched keywords, in the generated javascript, from ﷐:foo to cljs.core.Keyword.

Jared314
  • 5,181
  • 24
  • 30