[英]clojure.spec validation for fdef
I am trying to write a higher order function in clojure.spec with version 1.9.0-alpha11
and have not been able to get the validation to execute against the returned function. 我试图在版本
1.9.0-alpha11
clojure.spec中编写更高阶的函数,并且无法对返回的函数执行验证。
Using the example in the Higher order functions section of the clojure.spec guide, I define an adder
form as: 使用clojure.spec指南的高阶函数部分中的示例,我将
adder
表单定义为:
(defn adder [x] #(+ x %))
As described in the guide, I create a spec as: 如指南中所述,我创建了一个规范:
(s/fdef adder
:args (s/cat :x number?)
:ret (s/fspec :args (s/cat :y number?)
:ret number?)
:fn #(= (-> % :args :x) ((:ret %) 0)))
When I execute it in the repl, an exception is thrown instead of the desired spec validation error: 当我在repl中执行它时,抛出异常而不是所需的规范验证错误:
user> (def add2 (adder 2))
#'user/add2
user> (add2 2)
4
user> (add2 "s")
ClassCastException java.lang.String cannot be cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:128)
user>
I attempted to turn spec'ing on with (stest/instrument `adder)
. 我试图用
(stest/instrument `adder)
打开specining。 While this works for functions, it is not working for Higher order functions . 虽然这适用于函数,但它不适用于高阶函数 。
As it stands in clojure 1.9-alpha14 higher order functions cannot be validated in that way. 因为它位于clojure 1.9-alpha14中,所以无法以这种方式验证高阶函数。 Looking at
clojure.spec.test/instrument
, it is looking for functions in spec's registry, which will only include fdef'd functions. 看看
clojure.spec.test/instrument
,它正在查看spec的注册表中的函数,它只包含fdef'd函数。
The fspec in the :ret
will be used when testing adder
via clojure.spec.test/check
and alongside documenting the function that is the value of it. 当通过
clojure.spec.test/check
测试adder
时,将使用:ret
的fspec,并记录作为其值的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.