3

谁能解释一下,下面的例子有什么问题?为什么会抛出 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"])
4

1 回答 1

4

与 Alex Miller 在此 Google Groups 讨论中指出的类似,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}
于 2017-05-09T11:58:32.947 回答