Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tailRecM being not stack safe #460

Merged
merged 3 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class catzSpec extends catzSpecZIOBase {
implicit def ioArbitrary[A: Arbitrary]: Arbitrary[UIO[A]] = Arbitrary(genUIO[A])
MonadTests[UIO].apply[Int, Int, Int]
})
checkAllAsync("Monad[UIO]", implicit tc => ExtraMonadTests[UIO].monadExtras[Int])
checkAllAsync(
"ArrowChoice[ZIO]",
implicit tc => ArrowChoiceTests[ZIO[*, Int, *]].arrowChoice[Int, Int, Int, Int, Int, Int]
Expand Down
11 changes: 10 additions & 1 deletion zio-interop-cats/shared/src/main/scala/zio/interop/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private class CatsConcurrent[R] extends CatsMonadError[R, Throwable] with Concur
ZIO.uninterruptibleMask(_(fa).either.flatMap(f))
}

private class CatsMonadError[R, E] extends MonadError[ZIO[R, E, *], E] with StackSafeMonad[ZIO[R, E, *]] {
private class CatsMonadError[R, E] extends MonadError[ZIO[R, E, *], E] {
override final def pure[A](a: A): ZIO[R, E, A] = ZIO.succeedNow(a)
override final def map[A, B](fa: ZIO[R, E, A])(f: A => B): ZIO[R, E, B] = fa.map(f)
override final def flatMap[A, B](fa: ZIO[R, E, A])(f: A => ZIO[R, E, B]): ZIO[R, E, B] = fa.flatMap(f)
Expand All @@ -290,6 +290,15 @@ private class CatsMonadError[R, E] extends MonadError[ZIO[R, E, *], E] with Stac
override final def raiseError[A](e: E): ZIO[R, E, A] = ZIO.fail(e)

override final def attempt[A](fa: ZIO[R, E, A]): ZIO[R, E, Either[E, A]] = fa.either

override def tailRecM[A, B](a: A)(f: A => ZIO[R, E, Either[A, B]]): ZIO[R, E, B] = {
def loop(a: A): ZIO[R, E, B] = f(a).flatMap {
case Left(a) => loop(a)
case Right(b) => ZIO.succeedNow(b)
}

ZIO.effectSuspendTotal(loop(a))
}
}

/** lossy, throws away errors using the "first success" interpretation of SemigroupK */
Expand Down