开发者

How to make Clojure respect `*assert*` variable?

开发者 https://www.devze.com 2023-02-08 06:31 出处:网络
I was to understanding that Clojure\'s *assert* variable could be used to turn off assertions, but nothing I do seems to works.

I was to understanding that Clojure's *assert* variable could be used to turn off assertions, but nothing I do seems to works.

(defn foo [a]
  {:开发者_开发问答pre [(pos? a)]}
  (assert (even? a))
  [a])

(binding [*assert* false]
  (foo 1))
!! exception

(binding [*assert* false]
  (foo -2))
!! exception

Even to binding false when defining has same problems:

(binding [*assert* false]
  (defn bar [a]
    {:pre [(pos? a)]}
    (assert (even? a))
    [a]))

(bar 1)
!! execption

And then even to setting the variable direct does not working.

*assert*
is true

(alter-var-root (var *assert*) not)
*assert*
is still true

and

(var-set (var *assert*) false)
*assert*
is still true

So now I am not understanding what to do. I am confused.

Thank you.


*assert* is a compile-time variable, not a runtime variable. It's meant to be used with set! as a top-level statement, not with binding (of course unless you call eval inside the binding).


assert is macro defined in a way, that *assert* affects it's behavior at the expansion time.

if you try this code it will work as expected:

(binding [*assert* false]
   (eval '(assert false))
)

and your example with var-set should also work:

(var-set (var *assert*) false)
(assert false)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号