Fork me on GitHub
#clojurescript
<
2023-01-25
>
henrik14:01:11

Is it possible to avoid getting a compilation error without needing to fully qualify all non-shared keywords in a cljc file?

(:require
  #?(:clj  [some.clj-ns :as clj-ns]
     :cljs [some.cljs-ns :as cljs-ns]))

(def hello
  #?(:clj  ::clj-ns/hello
     :cljs ::cljs-ns/hello))

p-himik14:01:37

Haven't tested, but perhaps:

(:require
  #?(:clj  [some.clj-ns :as clj-ns :as-alias kw-ns]
     :cljs [some.cljs-ns :as cljs-ns :as-alias kw-ns]))

(def hello ::kw-ns/hello)

henrik14:01:08

As long as you use the same alias in both clj/cljs, you can get around it. But in the case where the clj branch uses a namespace that doesn’t appear in cljs, it doesn’t work. Another workaround seems to be to just create an empty cljs file for whatever namespace is used in the clj branch. The linter dislikes this solution, but it does make the compiler error go away.

p-himik14:01:22

Yeah, but have you tested it? Because in the example above I'm relying on :as-alias and not on :as.

p-himik14:01:10

Although, that should also be irrelevant and "just work" because the actual keyword uses an alias that should exist under both CLJ and CLJS.

henrik14:01:18

Ah, sorry I didn’t spot that. How about this situation:

(:require
  #?(:clj [some.clj-ns :as clj-ns]))

#?(:clj (def hello ::clj-ns/hello))

henrik14:01:27

The above is also a compilation error in cljs, which is mildly irritating.

Alex Miller (Clojure team)14:01:10

Reader conditionals still read everything. Auto resolved keywords require namespace alias state to resolve. Thus, any auto resolved keywords anywhere require aliases to exist (even if the read expression is not used).

henrik14:01:08

Perfectly logical, but somewhat undesirable when the CLJ/CLJS isn’t completely symmetrical.

Alex Miller (Clojure team)14:01:12

There is an ask Clojure question about this and it’s maybe something that could change, but not sure

👍 2
henrik14:01:28

The workarounds available seem to be, • Fully qualify • Create an empty CLJS file of whatever namespace is causing the problem

henrik14:01:37

Btw, amazing that you’ve indexed the Ask Clojure DB in your head @U064X3EF3

Alex Miller (Clojure team)14:01:53

AlexGPT3 has been trained on a complete data set of Clojure issues

😂 6
robert-stuttaford14:01:51

and is retrained continuously, i imagine 😂

Alex Miller (Clojure team)15:01:54

Another option: • Don't use autoresolved keywords

henrik15:01:26

Yeah, that’s what I meant with “fully qualify”. It’s the most reasonable option, but gets a little tedious with things like :com.wsscode.pathom3.connect.operation/input. You could also def it and use the var name to lower the tedium.

Carlo17:01:40

How would I convert a javascript Float32Array to a cljs vector?

Carlo17:01:26

This is the interop reference https://cljdoc.org/d/clojure-interop/cljs-web-api/1.0.10/api/js.Float32Array , and I can find a from function, but not something that works as a to

Carlo17:01:45

Ohhh, vec. I feel stupid now 😂