Fork me on GitHub
#lumo
<
2017-03-27
>
whitecoop20:03:55

Why doesn't this work?

$ cat lumo-test.cljs
(clojure.string/split "one\ntwo\nthree" #"\n")

$ lumo lumo-test.cljs
WARNING: No such namespace: clojure.string, could not locate clojure/string.cljs, clojure/string.cljc, or Closure namespace "" at line 1
And this does:
$ lumo

cljs.user=> (clojure.string/split "one\ntwo\nthree" #"\n")
["one" "two" "three"]
Adding a (require '[clojure.string :refer [split]]) makes everything fine for the lumo lumo-test.cljs, but it's not needed at the repl. Why is that?

anmonteiro20:03:00

@whitecoop rule of thumb is you should always require the stuff that you’re using

anmonteiro20:03:13

it happens to work at the REPL because the REPL namespace probably requires clojure.string

whitecoop20:03:41

@anmonteiro ah. hadn't thought of that possibility. that makes sense.

anmonteiro20:03:55

you need to require the stuff you use so that the CLJS compiler can figure out the dependency tree

whitecoop20:03:18

I've mostly thought of require as primarily a convenience to not have to write our the full name (`clojure.string/split` vs split, etc.) but, now that I'm thinking about it, I suppose (require '[some.lib]) is about building the dependency tree and using :refer, :as, and such inside of the require are the convenience part of the require.

whitecoop20:03:01

that was helpful, thanks

anmonteiro20:03:16

@whitecoop yeah, requires really need to be explicit

anmonteiro20:03:30

to be clear, this is a Google Closure limitation

anmonteiro20:03:42

we can’t have dynamic requires in ClojureScript like we can in Clojure

anmonteiro20:03:53

but even in Clojure you need to require stuff that you’re gonna use at some point

whitecoop21:03:33

@anmonteiro ok. I was wondering about the CLJ vs CLJS part of this. so is needing to be explicit because of Google Closure is the case even without dead code elimination? obviously, we'd want to write with that in mind, but thus far, running stuff with lumo some.cljs isn't using dead-code elimination, right?

anmonteiro21:03:21

@whitecoop yeah, this is not about dead code elimination

anmonteiro21:03:54

it’s about generating deps.js, which Closure needs to know which namespaces to load