我怀疑是否有一个类似于Prolog中的cut!能够阻止回溯的观念。考虑下面的规格
(s/def ::nested (s/keys :req-un [::a]))
(s/def ::a (s/keys :req-un [::b]))
(s/def ::b (s/keys :req-un [::c]))
(s/def ::c int?)
(s/def ::top-level (s/keys :req-un [::x]))
(s/def ::x int?)
(s/def ::thing (s/or :nested ::nested :top-level ::top-level))
(s/valid? ::thing {:x 3}) ; true
(s/valid? ::thing {:a {:b {:c 3}}}) ;true
(s/valid? ::thing {:a {:b {:c "not int"}}}) ;false
(s/explain ::thing {:a {:b {:c "not int"}}}) ;neither toplevel nor nested
这里{:a {:b {:c "not int"}}}
显然不符合::nested
或::toplevel
的规范,但一旦检查到::c
,它明显是一个未正确格式化的::nested
而不是一个未正确格式化的::toplevel
。
如果这种结构不存在,这是否应该在spec 2中考虑,或者根本不是所希望的吗?