Clojure 规范和解构?
Clojure Spec and destructuring?
不确定如何编写规范来检查函数的解构参数。
如果我有这个简单的功能:
(defn make-name [[first last]]
(str first " " last))
我为参数写了这个规范:
(s/def ::vstring2 (s/coll-of string? :kind vector? :count 2 :distinct false))
它工作正常:
(s/conform ::vstring2 ["Rich" "Hickey"])
=> ["Rich" "Hickey"]
但是当我尝试将它应用到函数时:
(s/fdef make-name
:args ::vstring2
:ret string?)
爆炸了:
(stest/check `make-name)
=>
({:spec #object[clojure.spec.alpha$fspec_impl$reify__2451
0x7dd4c5ac
"clojure.spec.alpha$fspec_impl$reify__2451@7dd4c5ac"],
:clojure.spec.test.check/ret {:result #error{:cause "Wrong number of args (2) passed to: roster/make-name",
(...)
如何使用 ::vstring2
编写函数规范?还是我?
谢谢。
不确定如何编写规范来检查函数的解构参数。
如果我有这个简单的功能:
(defn make-name [[first last]]
(str first " " last))
我为参数写了这个规范:
(s/def ::vstring2 (s/coll-of string? :kind vector? :count 2 :distinct false))
它工作正常:
(s/conform ::vstring2 ["Rich" "Hickey"])
=> ["Rich" "Hickey"]
但是当我尝试将它应用到函数时:
(s/fdef make-name
:args ::vstring2
:ret string?)
爆炸了:
(stest/check `make-name)
=>
({:spec #object[clojure.spec.alpha$fspec_impl$reify__2451
0x7dd4c5ac
"clojure.spec.alpha$fspec_impl$reify__2451@7dd4c5ac"],
:clojure.spec.test.check/ret {:result #error{:cause "Wrong number of args (2) passed to: roster/make-name",
(...)
如何使用 ::vstring2
编写函数规范?还是我?
谢谢。