clojure.spec conform throws stack overflow exception

时间:2017-05-09 11:01:54

标签: clojure clojure.spec

有人可以解释一下,下面这个例子有什么问题吗? 为什么抛出StackOverflowError异常?

(s/def ::tag keyword?)
(s/def ::s string?)
(s/def ::n number?)
(s/def ::g
  (s/cat :tag (s/? ::tag)
         :ex (s/alt :string ::s
                   :number ::n
                   :and (s/+ ::g)
                   )))


(s/conform ::g '["abc"])

1 个答案:

答案 0 :(得分:4)

与Alex Miller在this Google Groups discussion中指出的内容类似,s/+尝试在定义期间解析::g

这应该做你想要的,我想:

(s/def ::g
       (s/spec (s/cat :tag (s/? ::tag)
                      :ex (s/alt :string ::s
                                 :number ::n
                                 :and ::g))))

; REPL
user=> (s/conform ::g [:foo [:bar "abc"]])
{:ex [:and {:ex [:string "abc"] :tag :bar}] :tag :foo}