Can use reader tags with ClojureScript
In Clojure, adding custom reader tags is really easy
;; data_readers.clj (on classpath, eg. src/clj/)
{rd/qux datareaders.reader/my-reader}
;; Define a namespace containing the my-reader var:
(ns datareaders.reader)
(defn my-reader [x] 'y)
;; use special tag in other namespace. Readers have to be required first.
(require 'datareaders.reader)
(defn foo [x y]
(println #rd/qux x "prints y, not x due to reader tag."))
I am trying to do the same for ClojureScript, but I get an error that is #rd/qux
not defined. I am using lein cljsbuild once
to create a project. Is this a limitation of ClojureScript or is it what cljsbuild
builds the project before the readers decide? In this case, how can I get leiningen to load the readers namespace before running cljsbuild?
EDIT: Please note that this example intends to use reader tags in the ClojureScript source code, not when reading ancillary data through read-string
.
source to share
This is currently not possible, but will be as soon as # CLJS-1194 and # CLJS-1277 . Hopefully this will happen very soon.
If you want to do this, just rename data_readers.clj
to data_readers.cljc
and use conditional readers.
As an aside, what's your use case for this?
Both # CLJS-1194 and # CLJS-1277 , so this should work as expected.
source to share
The mechanism for adding a custom reader tag in cljs is different. You have to call register-tag-parser!
that takes a tag and fn.
From cljs reading tests :
(testing "Testing tag parsers"
(reader/register-tag-parser! 'foo identity)
(is (= [1 2] (reader/read-string "#foo [1 2]")))
Your example:
(cljs.reader/register-tag-parser! 'rd/qux (fn [x] 'y))
(defn foo [x y]
(println #rd/qux x "prints y, not x due to reader tag."))
source to share