On Tuesday, March 24, 2015 at 7:21:32 AM UTC-4, Thomas Heller wrote:
> >
> > Question: why exactly is this type hint needed when js/goog.DEBUG is used in an "if"? As far as I can see, js/goog.DEBUG is a boolean already.
> >
>
> Short answer: We don't evaluate or parse any Javascript when compiling ClojureScript. That means we don't know about goog.DEBUG and what type it may be. CLJS is parsed and analyzed so we know a whole lot about it without actually running anything.
I was curious and used the cljs compiler from [1] and compiled the code of the type annotated vs the non-type annotated. Using :advanced to compile this:
(if js/goog.DEBUG (println "(if goog.DEBUG...")) ;; no type hint
(if-not js/goog.DEBUG (println "(if-not goog.DEBUG...")) ;; no type hint
(when-debug (println "when-debug")) ;; when-debug from David
inspect the output you can see this output:
[... omitted ...]
$cljs$core$truth_$$(!1) && console.log("(if goog.DEBUG...");
!$cljs$core$truth_$$(!1) && console.log("(if-not goog.DEBUG...");
where the (when-debug ..) call is missing (as it should be). However the other two check cljs.core.truth_ for truthiness. Now the interesting part: Pasting the following code:
function $cljs$core$truth_$$($x$$54$$) {
return null != $x$$54$$ && !1 !== $x$$54$$;
}
function $cljs$core$not$$($x$$58$$) {
return $cljs$core$truth_$$($x$$58$$) ? !1 : !0;
}
$cljs$core$truth_$$(!1) && console.log("(if goog.DEBUG...");
!$cljs$core$truth_$$(!1) && console.log("(if-not goog.DEBUG...");
// Sometime also produces:
$cljs$core$not$$(!1) && console.log("(if-not goog.DEBUG...");
into:
https://closure-compiler.appspot.com/home
with advanced compilation will actually get rid of the truth_ call and elide it. HOWEVER, pasting the _entire_ javascript output produced by the cljs compiler into the same window (appspot) and the call remains in place. So does that mean there is potential to improve the google closure compiler? Or does it mean that there is other produced code by cljs that prevents this inlining to happen?
[1]
http://swannodette.github.io/2015/03/16/optimizing-clojurescript-function-invocation/