Fork me on GitHub
#clojure-spec
<
2018-09-04
>
djtango16:09:02

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

djtango16:09:45

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?))

djtango16:09:37

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

djtango16:09:58

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

djtango16:09:33

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

djtango16:09:43

Have I understood that right?

seancorfield17:09:12

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.

seancorfield17:09:54

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.

seancorfield17:09:04

(I hope I explained that accurately!)