When I try to add metadata to an endless lazy sequence in Clojure, I get a stack overflow, and if I delete the metadata, it will be fine. Why with-metadoes adding a macro interrupt lazy seq?
First create an infinite seq of a very nice number:
(defn good []
(lazy-seq
(cons 42
(good))))
user> (take 5 (good))
(42 42 42 42 42)
Then add some metadata to each of the lazy-seq instances:
(defn bad []
(lazy-seq
(cons 42
(with-meta
(bad)
{: padding 4}))))
user> (take 5 (bad))
java.lang.StackOverflowError (NO_SOURCE_FILE: 0)
[Thrown class clojure.lang.Compiler $ CompilerException]
Try moving the metadata one level:
(defn also-bad []
(with-meta
(lazy-seq
(cons 42
(also-bad)))
{:padding 4}))
user> (take 5 (foo))
java.lang.StackOverflowError (NO_SOURCE_FILE:0)
[Thrown class clojure.lang.Compiler$CompilerException]
:
(defn also-works []
(lazy-seq
(cons 4
(with-meta
()
{:a 5}))))
user> (also-works)
(4)
user> (meta (rest (also-works)))
{:a 5}
user>