Fork me on GitHub

Has anyone come across issues with reader conditionals and spec in cljc?


Say I want to include a spec from a clj-only library can I do this in cljc:

#?(:clj (s/def ::my-spec ::clj-only-lib/the-spec)
   :cljs (s/def ::my-spec any?))


currently we are seeing this exception: Caused by: clojure.lang.ExceptionInfo: Invalid keyword: ::clj-time.s/local-date. {:type :reader-exception, :ex-kind :reader-error, :file "/x/spec.cljc", :line 16, :col 54}

Alex Miller (Clojure team)16:09:52

Autoresolved keywords use the current env at read-time to resolve the ns

Alex Miller (Clojure team)16:09:22

And this is reading, which happens regardless of the chosen branch

Alex Miller (Clojure team)16:09:47

A workaround is to use the full keyword with namespace


Ah so the reader for your cljs doesn't ignore the clj branches?

Alex Miller (Clojure team)21:09:32

It’s read before it’s ignored


and so it gets to the auto-resolved keywords and if the clj-only ns isn't in the env at that point you get an exception


Have I understood that right?


Yes, the expressions are read and auto-resolving keywords is part of that, prior to the conditional being applied. Then the result of reading is compiled.


So reader conditionals can only select between syntactically valid expressions that can be read in -- :some-ns/some-thing is read "as-is" but ::some-alias/some-thing has some-alias expanded as part of the reading process.


(I hope I explained that accurately!)