Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macros Clojure断言在编译时不为零?_Macros_Clojure_Compile Time - Fatal编程技术网

Macros Clojure断言在编译时不为零?

Macros Clojure断言在编译时不为零?,macros,clojure,compile-time,Macros,Clojure,Compile Time,我们可以看到Clojure中的函数在运行时不是nil (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找到一种方法 (defmacro type

我们可以看到Clojure中的函数在运行时不是nil

(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找到一种方法

(defmacro typeof 
  ([expression]
    (:class (expression-info expression)))

(defmacro primitive? 
  ([expression]
    (:primitive? (expression-info expression))))
以下是我对这个问题的看法:

我的问题是-有没有办法在Clojure中检查“常量”在编译时是否为零?


(我意识到这个问题可能看起来很琐碎——我想用它作为后续关于Clojure中编译时类型检查问题的构建块)

如果“常量”是指存储在全局
var

(def my-nullable nil)
可在编译时解决的问题,然后:

(defmacro compile-time-var-nil-check [x] 
  (let [x @(resolve x)] 
    (assert (not (nil? x)))))
(defmacro compile-time-var-nil-check [x] 
  (let [x @(resolve x)] 
    (assert (not (nil? x)))))