Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can reader tags be used with ClojureScript

In Clojure, adding custom reader tags is really simple

;; 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 achieve the same thing for ClojureScript but am getting an error that #rd/qux is not defined. I am using lein cljsbuild once to build the project. Is that a limitation of ClojureScript or is it that cljsbuild builds the project before the readers have been resolved? In that case, how can I force leiningen to load the readers namespace before cljsbuild is started?

EDIT: Note that this example intends to use reader tags within ClojureScript source code and not when reading auxilliary data via read-string.

like image 730
shaft Avatar asked Sep 28 '22 08:09

shaft


1 Answers

This currently isn't possible, but will be as soon as #CLJS-1194 and #CLJS-1277 are fixed. Hopefully that will happen very soon.

If you wanted to do it, 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 are fixed, so this should work as expected.

like image 194
bostonou Avatar answered Oct 30 '22 02:10

bostonou