Bootstrap Asserts

May 8, 2016

In Clojure, you can disable asserts by setting the *assert* var to false. Once set, this affects any newly compiled code which uses the assert macro. For example, normally

(macroexpand '(assert (= 1 2) "hi"))

yields (reformatted for whitespace):

(if (= 1 2) 
  nil 
  (do 
    (throw 
      (new java.lang.AssertionError 
        (clojure.core/str 
          "Assert failed: " 
          "hi" "\n" 
          (clojure.core/pr-str 
            (quote (= 1 2))))))))

But if you first set *assert* to false, then the assert macro will expand to nil.




In ClojureScript, on the other hand, with macros being expanded via Clojure, setting *assert* (either at the REPL or in your ClojureScript code) cannot affect the macroexpansion. Instead, you must rely on the :elide-asserts compiler option.

But in bootstrap ClojureScript, since macros are compiled as ClojureScript, in the same environment with the code, setting *assert* in self-hosted environments works just as it does in Clojure.

Try it in your favorite bootstrap REPL! (I've checked and it works in clojurescript.io, Plank, and Replete—it should work in any self-hosted environment)

Tags: ClojureScript Bootstrap