我在想是否有类似于 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 中考虑的,或者这不是期望的事物?