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

Add IO#to conversion method #50

Merged
merged 7 commits into from
May 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 9 additions & 10 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package effect

import cats.effect.internals.{AndThen, IOPlatform, NonFatal}
import scala.annotation.tailrec
import scala.annotation.unchecked.{uncheckedVariance => uV}
import scala.annotation.unchecked.uncheckedVariance
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.concurrent.duration._
import scala.util.{Left, Right}
Expand Down Expand Up @@ -385,18 +385,17 @@ sealed abstract class IO[+A] {
* Converts the source `IO` into any `F` type that implements
* the [[cats.effect.Async Async]] type class.
*/
final def to[F[_]](implicit F: cats.effect.Async[F]): F[A @uV] =
final def to[F[_]](implicit F: cats.effect.Async[F]): F[A @uncheckedVariance] =
this match {
case Pure(a) => F.pure(a)
case RaiseError(e) => F.raiseError(e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This is exactly what I had in mind.

case _ => F.suspend {
// Evaluating until finished or the first async boundary
unsafeStep match {
case Pure(a) => F.pure(a)
case RaiseError(e) => F.raiseError(e)
case async => F.async(async.unsafeRunAsync)
}
}
case Suspend(thunk) => F.suspend(thunk(()).to[F])
case Async(k) => F.async(k)

case BindSuspend(thunk, f) =>
F.flatMap(F.suspend(thunk(()).to[F]))(e => f(e).to[F])
case BindAsync(k, f) =>
F.flatMap(F.async(k))(e => f(e).to[F])
}

override def toString = this match {
Expand Down
8 changes: 8 additions & 0 deletions core/shared/src/test/scala/cats/effect/Generators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ object Generators {
10 -> genFlatMap[A])
}

def genSyncIO[A: Arbitrary: Cogen]: Gen[IO[A]] = {
Gen.frequency(
5 -> genPure[A],
5 -> genApply[A],
1 -> genFail[A],
10 -> genFlatMap[A])
}

def genPure[A: Arbitrary]: Gen[IO[A]] = arbitrary[A].map(IO.pure(_))

def genApply[A: Arbitrary]: Gen[IO[A]] = arbitrary[A].map(IO.apply(_))
Expand Down
28 changes: 19 additions & 9 deletions laws/shared/src/test/scala/cats/effect/IOTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,27 @@ class IOTests extends BaseTestsSuite {
f.value shouldEqual Some(Success(100))
}

testAsync("io.to[IO] works") { implicit ec =>
def repeatedTransformLoop[A](n: Int, io: IO[A]): IO[A] =
io.to[IO].flatMap { x =>
if (n <= 0) IO.pure(x).to[IO]
else repeatedTransformLoop(n - 1, io)
}

testAsync("io.to[IO] <-> io") { implicit ec =>
check { (io: IO[Int]) => io.to[IO] <-> io }
}

testAsync("sync.to[IO] is stack-safe") { implicit ec =>
// Override default generator to only generate
// synchronous instances that are stack-safe
implicit val arbIO: Arbitrary[IO[Int]] =
Arbitrary(Gen.delay(genSyncIO[Int]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, with the new implementation, does this work even with the async-including generator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's the same thing.

Depending on F[_] it might end up being stack safe, but only if F.async would make it so, but we've established that F.async shouldn't do that, which sort of makes sense (I was just complaining that we should have two versions of async in there for the price of 1 :))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks.


check { (io: IO[Int]) =>
repeatedTransformLoop(10000, io) <-> io
}
}

testAsync("async.to[IO] is stack-safe if the source is") { implicit ec =>
// Stack-safe async IO required
def async(a: Int) = IO.async[Int] { cb =>
Expand All @@ -365,15 +382,8 @@ class IOTests extends BaseTestsSuite {
})
}

def loop(n: Int, io: IO[Int]): IO[Int] =
io.to[IO].flatMap { x =>
if (n <= 0) IO.pure(x).to[IO]
else loop(n - 1, io)
}

val f = loop(10000, async(99)).unsafeToFuture()
val f = repeatedTransformLoop(10000, async(99)).unsafeToFuture()
ec.tick()

f.value shouldEqual Some(Success(99))
}
}
Expand Down