Clojurians
#cljs-dev
<
2015-11-09
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

maria09:11:15

When compiling a file with a "if expression” which contains constants in the then/else branches, the resulting js file has empty then and else branches. Whereas, when the then/else branches contain for example vectors, the then/else branches contain the vectors. The following gist shows the behaviour I am trying to describe: https://gist.github.com/mneise/9a94f49f2960d5ee1445. It seems that constants are not emitted when the context is a statement (https://github.com/clojure/clojurescript/blob/master/src/main/clojure/cljs/compiler.cljc#L418). Does someone know why this is the case?

thheller09:11:26

@maria the analyzer does not correctly identify the vector (or anything beyond basic types) as a constant

thheller09:11:20

the empty branches are due to the fact that if (true) { 2 } else { 1 } is pointless

thheller09:11:38

since you cannot x = if (true) { 2 } else { 1 } in js

thheller09:11:29

when the result of a branch is needed the compiler will emit x = pred ? true : false;

maria09:11:59

yeah, just tried that, too :simple_smile: Cool, makes sense, thank you for explaining. And I guess you can’t completely remove it, since the test expression could have side effects?

thheller10:11:42

David suggested at one point that the analyzer should be able to identify vectors/maps as constants

thheller10:11:55

but I'm not aware of any work in that area

maria10:11:16

And how does it work in the REPL? Everything that gets passed to eval is handled as an expression?

thheller10:11:17

uhm cljs.repl is "cheating"

thheller10:11:57

if you enter (if (= 2 2) 1 2) it will eval (pr-str (if (= 2 2) 1 2))

thheller10:11:08

so the if no longer is a :statement

maria10:11:15

ah, interesting

maria10:11:24

cool, thank you :simple_smile:

thheller10:11:41

@maria you must made me aware of a bug in my REPL implementation. thx :simple_smile:

maria10:11:25

@thheller: no problem :smile:

thheller10:11:56

I actually turn everything into a an :expression before emitting code (not wrap it) but if with constants creates invalid code :simple_smile: