为了了解情况,如果您为 '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")