【发布时间】:2014-03-15 04:01:17
【问题描述】:
我们可以在 Clojure 中看到 a way to use design by contract to check that arguments 到 Clojure 中的函数在运行时不是零。
(defn constrained-fn [ x]
{:pre [(not (nil? x))]}
x)
(constrained-fn nil)
=> AssertionError Assert failed: (not (nil? x)) ...../constrained-fn (form-init5503436370123861447.clj:1)
我们可以在 Clojure 中看到check the type of an expression at compile time 的方法
(defmacro typeof
([expression]
(:class (expression-info expression)))
(defmacro primitive?
([expression]
(:primitive? (expression-info expression))))
这是我对基于compile-time macros in Clojure的问题的看法:
(defmacro compile-time-nil-check [x] (assert (not (nil? x))))
(def my-nullable nil)
(compile-time-nil-check my-nullable)
我的问题是 - 有没有办法在 Clojure 的编译时检查“常量”是否不为零?
(我意识到这个问题可能看起来微不足道——我想用它作为后续问题的构建块,以解决有关 Clojure 中编译时类型检查的后续问题)。
【问题讨论】:
标签: macros clojure compile-time