@@ -166,7 +166,7 @@ import scala.runtime.Statics
166
166
* loop("Iterator3: ", it3.next(), it3)
167
167
* }}}
168
168
*
169
- * - In the `fibs` example earlier, the fact that `tail` works at all is of interest.
169
+ * In the `fibs` example earlier, the fact that `tail` works at all is of interest.
170
170
* `fibs` has an initial `(0, 1, LazyList(...))`, so `tail` is deterministic.
171
171
* If we defined `fibs` such that only `0` were concretely known, then the act
172
172
* of determining `tail` would require the evaluation of `tail`, so the
@@ -218,6 +218,27 @@ import scala.runtime.Statics
218
218
* filtered.isEmpty // prints "getting empty LazyList"
219
219
* }}}
220
220
*
221
+ * ----
222
+ *
223
+ * You may sometimes encounter an exception like the following:
224
+ *
225
+ * {{{
226
+ * java.lang.RuntimeException: "LazyList evaluation depends on its own result (self-reference); see docs for more info
227
+ * }}}
228
+ *
229
+ * This exception occurs when a `LazyList` is attempting to derive its next element
230
+ * from itself, and is attempting to read the element currently being evaluated. A
231
+ * trivial example of such might be
232
+ *
233
+ * {{{
234
+ * lazy val a: LazyList[Int] = 1 #:: 2 #:: a.filter(_ > 2)
235
+ * }}}
236
+ *
237
+ * When attempting to evaluate the third element of `a`, it will skip the first two
238
+ * elements and read the third, but that element is already being evaluated. This is
239
+ * often caused by a subtle logic error; in this case, using `>=` in the `filter`
240
+ * would fix the error.
241
+ *
221
242
* @tparam A the type of the elements contained in this lazy list.
222
243
*
223
244
* @see [[https://docs.scala-lang.org/overviews/collections-2.13/concrete-immutable-collection-classes.html#lazylists "Scala's Collection Library overview" ]]
@@ -253,7 +274,9 @@ final class LazyList[+A] private(private[this] var lazyState: () => LazyList.Sta
253
274
// if it's already mid-evaluation, we're stuck in an infinite
254
275
// self-referential loop (also it's empty)
255
276
if (midEvaluation) {
256
- throw new RuntimeException (" self-referential LazyList or a derivation thereof has no more elements" )
277
+ throw new RuntimeException (
278
+ " LazyList evaluation depends on its own result (self-reference); see docs for more info"
279
+ )
257
280
}
258
281
midEvaluation = true
259
282
val res = try lazyState() finally midEvaluation = false
0 commit comments