Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active March 1, 2018 04:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfikes/002a157c484be9fe9a96531d35216e3c to your computer and use it in GitHub Desktop.
Save mfikes/002a157c484be9fe9a96531d35216e3c to your computer and use it in GitHub Desktop.
Bad codegen

The following code should evaluate to true:

((or int? string?) 1)

But instead it evaluates to the int? function. In other words this evaluates to true:

(= int? ((or int? string?) 1))

What???

This is because the JavaScript generated for ((or int? string?) 1) is

(cljs.core.int_QMARK_) || (cljs.core.string_QMARK_).call(null,(1))

which short-circuits, evaluating to cljs.core.int_QMARK_. The fix is the addition of some needed parens.

This likewise affects and. But to see that you need a prefix operator.

If you evaluate this code

(js-delete (and int? string?) "bogus-property")

it should be the same as

(js-delete string? "bogus-property")

which is harmless. But the JavaScript generated for the original form is

delete (cljs.core.int_QMARK_) && (cljs.core.string_QMARK_)["bogus-property"]

and, poof!, the int? function is gone with the wind. This now evaluates to false.

(exists? int?)

Fixes for these landed in ClojureScript master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment