plumatic / dommy

A tiny ClojureScript DOM manipulation and event library
759 stars 74 forks source link

Type hinting for predicates #23

Closed dubiousdavid closed 11 years ago

dubiousdavid commented 11 years ago

Predicates should have a ^boolean type hint so that they can be properly optimized by the ClojureScript compiler.

aria42 commented 11 years ago

Can you elaborate with an example? What's the difference in generated js? Happy to do this if I just understand what the gains might be.

dubiousdavid commented 11 years ago

I learned this after watching Fogus' talk on ClojureScript. Also, you'll notice that all predicates in the cljs.core have this type hint. I can't remember exactly how it works.

On Apr 2, 2013, at 1:20 PM, Aria Haghighi notifications@github.com wrote:

Can you elaborate with an example? What's the difference in generated js? Happy to do this if I just understand what the gains might be.

— Reply to this email directly or view it on GitHub.

cpetzold commented 11 years ago

Here's an example:

(defn ^boolean typed? [x] x)
(defn not-typed? [x] x)
(when (typed? true))
(when (not-typed? true))

.. compiles to

typed_QMARK_ = function typed_QMARK_(x) {
  return x
};
not_typed_QMARK_ = function not_typed_QMARK_(x) {
  return x
};
if(typed_QMARK_.call(null, true)) {
}else {
}
if(cljs.core.truth_(not_typed_QMARK_.call(null, true))) {
}else {
}

You'll notice that in the when, typed? isn't required to be wrapped in cljs.core.truth_.

aria42 commented 11 years ago

I'd accept a pull request with the desired changes.