Skip to content

Commit 3f48848

Browse files
committed
Merge pull request #923 from TomasMikula/trailrec-eval
Make Call.loop @tailrec optimized.
2 parents 92a3c5c + bd7fe87 commit 3f48848

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

core/src/main/scala/cats/Eval.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,23 @@ object Eval extends EvalInstances {
228228

229229
object Call {
230230
/** Collapse the call stack for eager evaluations */
231-
private def loop[A](fa: Eval[A]): Eval[A] = fa match {
231+
@tailrec private def loop[A](fa: Eval[A]): Eval[A] = fa match {
232232
case call: Eval.Call[A] =>
233233
loop(call.thunk())
234234
case compute: Eval.Compute[A] =>
235235
new Eval.Compute[A] {
236236
type Start = compute.Start
237237
val start: () => Eval[Start] = () => compute.start()
238-
val run: Start => Eval[A] = s => loop(compute.run(s))
238+
val run: Start => Eval[A] = s => loop1(compute.run(s))
239239
}
240240
case other => other
241241
}
242+
243+
/**
244+
* Alias for loop that can be called in a non-tail position
245+
* from an otherwise tailrec-optimized loop.
246+
*/
247+
private def loop1[A](fa: Eval[A]): Eval[A] = loop(fa)
242248
}
243249

244250
/**

0 commit comments

Comments
 (0)