Fork me on GitHub
#clojurescript
<
2021-03-28
>
Sam Ritchie03:03:30

Hey all - curious if this is a bug, or behavior, that anyone here has encountered before?

(map (fn [e_i e-i] [e_i e-i])
     [1 2 3] [4 5 6])
;;=> ([4 4] [5 5] [6 6])

Sam Ritchie03:03:13

it’s exactly what would happen if I had used the same arg name - I had forgotten that, of course, dashes get mapped to underscores

andy.fingerhut05:03:51

Definitely ClojureScript-specific behavior, but makes sense if all of these local names must become mapped to legal JavaScript names. I suppose there are techniques that could generate guaranteed-unique names, but they would have to change some names, which for non-local names would make cljs/js interop more tricky, I would guess.

andy.fingerhut05:03:24

Same thing happens for Clojure namespace names -> file names for require, and for Java class names generated from Clojure names.

alexmiller05:03:32

We fixed something like this in Clojure back around 1.6

lilactown18:03:19

seems like a bug

introom07:03:49

I started an rn app (written in cljs) and uses shadow repl to connect to it. When typing (prn ns), it gives nil. Why nil instead of some namespace?

raspasov08:03:00

I believe this is specific to ClojureScript; as per the CLJS docs about *ns*:

Var bound to the current namespace. Only used for bootstrapping.

raspasov08:03:14

Try creating a scratch.cljc file like this; and then load it in another namespace; if you call (shared.scratch/macro-1) from that namespace, you should get the desired result.

raspasov08:03:52

Note: it needs to be a .cljc file (and not .cljs)

thheller08:03:18

yeah, *ns* is available in macros but otherwise not available in CLJS

introom08:03:04

Then how can one know which ns it is currently in ?

raspasov08:03:29

@UGC0NEP4Y you have to use a macro; the example above works.

thheller08:03:38

in the REPL the prompt prints it. in Cursive it is shown at the bottom right in the REPL input window. otherwise a quick trick is just evaling ::foo, that'll give you :cljs.user/foo or so

raspasov08:03:31

I meant you need to use a macro if you want to figure out which ns you’re “in” at runtime; otherwise during development, depends on the editor, yeah

thheller08:03:22

there is no such thing as being "in" an ns at runtime

raspasov08:03:53

Yeah that’s why I put it in quotes 🙂

thheller09:03:00

FWIW *ns* is a binding which are problematic in CLJS due to the async nature. It is also normally a clojure.lang.Namespace instance which CLJS doesn't have. so it is better to just not have it or rely on it 🙂

raspasov09:03:26

Interesting; so even in a macro not a good idea?

thheller09:03:34

in macro it is fine. that is not runtime, that is compile time and running in CLJ

raspasov09:03:47

Ah, got it. Cool.

raspasov09:03:58

Found this weird trick on SO:

(apply str (drop-last 2 (str `_)))

thheller09:03:24

(symbol (namespace ::foo))

👌 1