背景:如果您为 'alt' 规约提供了无效值,explain-data 将返回两个问题
(s/explain-data (s/cat
:x (s/alt :i int? :s string?))
[:kw])
;; explain-data 包含两个问题,每个替代项一个问题
;; #:clojure.spec.alpha{:problems ({:path [:x :i], :pred clojure.core/int?, :val :kw, :via [], :in [0]} {:path [:x :s], :pred clojure.core/string?, :val :kw, :via [], :in [0]}), :spec {:clojure.spec.alpha/op :clojure.spec.alpha/pcat, :ps [{:clojure.spec.alpha/op :clojure.spec.alpha/alt, :ps (#function[clojure.core/int?] #function[clojure.core/string?--5132]), :ks (:i :s), :forms (clojure.core/int? clojure.core/string?), :id #uuid "88448dbf-dce7-4789-9266-aa150a6563bc"}], :ret {}, :ks [:x], :forms [(clojure.spec.alpha/alt :i clojure.core/int? :s clojure.core/string?)], :rep+ nil}, :value [:kw]}
但是,如果值缺失一个元素,explain-data 返回一个问题
(s/explain-data (s/cat
:x (s/alt :i int? :s string?))
[])
;; explain-data 包含一个复合谓词的问题
;; #:clojure.spec.alpha{:problems [{:path [:x], :reason "Insufficient input", :pred (clojure.spec.alpha/alt :i clojure.core/int? :s clojure.core/string?), :val (), :via [], :in []}], :spec {:clojure.spec.alpha/op :clojure.spec.alpha/pcat, :ps [{:clojure.spec.alpha/op :clojure.spec.alpha/alt, :ps (#function[clojure.core/int?] #function[clojure.core/string?--5132]), :ks (:i :s), :forms (clojure.core/int? clojure.core/string?), :id #uuid "5f655d8c-19bb-43de-a153-722dd338d1f3"}], :ret {}, :ks [:x], :forms [(clojure.spec.alpha/alt :i clojure.core/int? :s clojure.core/string?)], :rep+ nil}, :value []}
动机:对于具有许多替代项的宏的规约,缺失元素的错误相当晦涩。例如,尝试 (defn hello "hello world")