根据这篇SO帖子,我想在我的函数中打印前置条件的值。然而,在以下情况下它会失败(可能是解构):
我有一个dir?
助手函数(可以跳过这个):
(defn dir? [s]
"returns true if the string passed is is an existing directory"
(->> (clojure.java.io/file s)
((juxt #(.exists %) #(.isDirectory %)))
(every? true?)))
它运行得很好,使用is
宏,我可以获得一些漂亮的错误消息,其中我可以看到测试和传递的参数:
(is (dir? (io/file "resources/static"))) ;; => true
(is (dir? (io/file "resources/statice"))) ;; typo, error below
在clojure.lang.PersistentList$EmptyList@1(boot.user4515592986834245937.clj:86)中失败,期望:(dir?(io / file“resources / statice”))实际:(not(dir?#object [java.io.File 0x6730a420“resources / statice”]))
但是,当我尝试在先决条件:pre
中使用它时,我得到了一个丑陋的错误:
(defn make-something
[&{:keys [dir]
:or {dir "default-dir"}}]
{:pre [(is (dir? (clojure.java.io/file dir)))]}
;;... do something with these
)
(make-something :dir "resources/statices") ;; doesn't exist
clojure.lang.Compiler$CompilerException: java.lang.AssertionError: 断言失败:(is (dir?(io / file dir))), 编译:(boot.user4515592986834245937.clj:80:12) java.lang.AssertionError: 断言失败:(is (dir?(io / file dir)))
我该如何在函数中获得一个像上面这样的漂亮错误消息?
如果有影响的话,我正在使用Clojure 1.7。