尽管使用了gensym

时间:2016-02-26 16:34:36

标签: clojure

当我尝试运行以下代码

时,我收到运行时异常
(ns valer)


(defmacro validate
 [m & fields]
 (if (not= 0 (mod (count fields) 3))
  (throw (IllegalArgumentException. "clauses not a set of 3 fields"))
 (let [ partitions (partition 3 fields) ]
   (if (= 0 (count partitions))
     true
     (let [v  (first partitions)
           f (first v)
           a (nth v 1)
           s (nth v 2) ]
      `(try
         (let [ r# (apply ~f ~(conj a m)) ]
           (if r#
             (validate ~m ~@(mapcat concat (rest partitions)))
             (throw (IllegalArgumentException. ~s))))
         (catch Exception e#
           (throw (IllegalArgumentException. ~s e#)))))))))
(def q 1)

(defn g [a b] (> a b))
(defn m [a] (if (not (odd? a))
              (throw (Exception. "even number found."))))
(macroexpand (validate q
              g [5] "number is not greater than 5"
              m [] "number must be odd"))

例外是

CompilerException java.lang.RuntimeException: Can't let qualified name: valer/_, compiling:(/Users/gunslinger/concur/testv.clj:29:14) 

我的猜测是〜(魔法师)或者r#必须是罪魁祸首,虽然我不确定。有人可以如何解决此错误? validate宏将带有一个参数mand(fn vec_o​​f_args error_msg)列表将fn应用于vec_o​​f_args m,如果它返回false或抛出异常则引发异常。

0 个答案:

没有答案