当体包含宏并抛出异常时,lazy-seq 似乎评估不一致。第一次评估抛出异常,随后的返回空序列。
演示代码
`
(defn gen-lazy []
(let [coll [1 2 3]]
(lazy-seq
(when-let [s (seq coll)]
(throw (Exception.))))))
(def lazy (gen-lazy))
(try
(println "lazy:" lazy)
(catch Exception ex
(println ex)))
(try
(println "lazy, again:" lazy)
(catch Exception ex
(println ex)))
`
它应该在两次都抛出异常,但实际上只在第一次抛出。一般来说,一个表达式不应当在之前被评估的情况下评估为不同的值。
当移除闭包...
`
(defn gen-lazy []
(lazy-seq
(when-let [s (seq [1 2 3])]
(throw (Exception.)))))
`
...或者移除 when-let 宏...
`
(defn gen-lazy []
(let [coll [1 2 3]]
(lazy-seq
(seq coll)
(throw (Exception.)))))
`
它就能正常工作,即始终抛出异常,因此似乎存在闭包和宏之间的某种交互作用。这种特定的组合在 'map' 函数中使用。
另请参阅: https://groups.google.com/forum/?fromgroups=#!topic/clojure/Z3EiBUQ7Inc