Fork me on GitHub
#clojurescript
<
2021-07-22
>
misha05:07:21

$ planck
ClojureScript 1.10.339
cljs.user=> ([1 2] nil)
1
cljs.user=> (get [1 2] nil)
nil

🙌 4
😮 2
raspasov12:07:20

There’s probably a good explanation for this, but I don’t know it.

raspasov12:07:28

FWIW, in JVM Clojure ([1 2] nil) returns Execution error (IllegalArgumentException)… Key must be integer

dnolen13:07:13

the first case - the second case isn't different from Clojure

dnolen13:07:06

Fixed

bellissimo 14
💥 2
Charles Comstock22:07:08

Does anyone know if there is an equivalent library (or builtin) to https://github.com/clojure-goes-fast/clj-java-decompiler for ClojureScript which emits the javascript generated from a particular s-expression? Having that tooling available is really nice for both performance optimization and understanding the compiler. I appreciate that whatever is generated is then transformed through the Closure compiler for further optimization but I still think it would be useful. Anyone aware of anything in that vein?

dpsutton22:07:37

% clj -A:cljs -M -m cljs.main -r
ClojureScript 1.10.773
cljs.user=> (set! *print-fn-bodies* true)
true
cljs.user=> (defn foo [x] (inc x))
#'cljs.user/foo
cljs.user=> foo
#object[cljs$user$foo "function cljs$user$foo(x){
return (x + (1));
}"]
cljs.user=>

Charles Comstock23:07:17

oh interesting I was not aware of that, I will experiment with that, thank you!

Charles Comstock23:07:56

Oh so that's only how it prints the function, is there a way to access that data?

Charles Comstock23:07:23

(to run it through a pretty printer say?)

Charles Comstock02:07:13

Oh ... it's literally just calling str on the function itself. So that's all that is needed to get the value. That makes perfect sense but was not at all apparent. Anyway thank you for your help!

thheller06:07:16

on the right there is a JS Code tab to show the generated code

thheller06:07:36

not its primary purpose but might help

cfleming22:07:35

When connecting to a REPL for the first time, I’d like to make a best-effort test to detect if the REPL is a CLJS one or CLJ. I’m having difficulty working out how to do this - my CLJS-fu is too weak to figure out the var reification issues. I had planned to use `(when-let [core (find-ns 'cljs.core)] (ns-resolve core 'clojurescript-version))` - this works in Clojure but not in CLJS (no ns-resolve). If I just do `(when (find-ns 'cljs.core) cljs.core/clojurescript-version)` then that fails to compile in Clojure. I’ve tried various permutations using var and the like with no success. Is there something I could use for this?

cfleming23:07:18

I’d rather not use reader conditionals if possible since that will fail when connecting to older Clojure systems.

Jimmy Miller23:07:45

How hacky do you want to be? For example (mod 5 0) throws an error in clj but returns NaN in cljs.

🎩 3
😱 5
cfleming23:07:01

Haha, I’d rather not be hacky if possible 🙂

Jimmy Miller23:07:02

The NaN behavior is in the docstring too. So this isn’t undocumented behavior.

cfleming23:07:24

Sure, but I don’t care about that distinction as much. This is for Cursive, so e.g. I don’t want to be setting things like locals clearing for CLJS REPLs.

cfleming23:07:30

I also need to detect Babashka as a REPL server type, so things like the mod hack start getting very fiddly very fast - I’d rather something a bit more definitive like *clojurescript-version* if I can make it work.

Jimmy Miller23:07:09

Would (resolve 'cljs.core/*clojurescript-version*) not work? Seems to work in my repls. Returns nil in clj and something in cljs

cfleming23:07:09

Hmm, you’re right, I’d swear I had tried that.

cfleming23:07:02

Ohhh, you know what? I did some initial testing using an in-browser CLJS REPL, and that doesn’t work there. But I think I can live with not supporting self-hosted CLJS for now.

cfleming23:07:57

That does work in Planck and Lumo, though - maybe http://clojurescript.io is funky for some reason.

cfleming23:07:33

Yeah, it fails in http://clojurescript.net, too. Still, I’m not too worried about that, I think we have a winner. Thanks!

👍 4
4
thheller06:07:28

don't use those pages. they are severely outdated and do not reflect the current state of CLJS

thheller06:07:12

one quick way to test would be just (str #'assoc)

nice 3
thheller06:07:15

(str #'assoc)
=> "#'cljs.core/assoc"

thheller06:07:29

(str #'assoc)
=> "#'clojure.core/assoc"

thheller06:07:13

or work on the var directly but not sure they have the exact same API in CLJ/S

thheller06:07:28

any core var you know exists in both dialects is fair game of course, doesn't have to be assoc

thheller06:07:40

CLJS will always have cljs.core as ns

cfleming07:07:58

Interesting idea also, thanks!