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 all commits
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
6 changes: 6 additions & 0 deletions core/shared/src/main/scala/cats/effect/Effect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ trait Effect[F[_]] extends Async[F] with LiftIO[F] {
})
}
}

override def liftIO[A](ioa: IO[A]): F[A] = {
// Implementation for `IO#to` depends on the `Async` type class,
// and not on `Effect`, so this shouldn't create a cyclic dependency
Copy link
Member

Choose a reason for hiding this comment

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

It's being overridden anyway.

ioa.to[F](this)
}
}
22 changes: 20 additions & 2 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package effect

import cats.effect.internals.{AndThen, IOPlatform, NonFatal}
import scala.annotation.tailrec
import scala.annotation.unchecked.uncheckedVariance
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.concurrent.duration._
import scala.util.{Left, Right}
Expand Down Expand Up @@ -380,6 +381,23 @@ sealed abstract class IO[+A] {
p.future
}

/**
* 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 @uncheckedVariance] =
this match {
case Pure(a) => F.pure(a)
case RaiseError(e) => F.raiseError(e)
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 {
case Pure(a) => s"IO($a)"
case RaiseError(e) => s"IO(throw $e)"
Expand Down Expand Up @@ -426,7 +444,7 @@ private[effect] trait IOInstances extends IOLowPriorityInstances {

override def shift[A](ioa: IO[A])(implicit ec: ExecutionContext) = ioa.shift

def liftIO[A](ioa: IO[A]) = ioa
override def liftIO[A](ioa: IO[A]) = ioa
}

implicit def ioMonoid[A: Monoid]: Monoid[IO[A]] = new IOSemigroup[A] with Monoid[IO[A]] {
Expand Down Expand Up @@ -591,7 +609,7 @@ object IO extends IOInstances {

private final case class Pure[+A](a: A)
extends IO[A]
private final case class RaiseError(t: Throwable)
private final case class RaiseError(e: Throwable)
extends IO[Nothing]
private final case class Suspend[+A](thunk: AndThen[Unit, IO[A]])
extends IO[A]
Expand Down
39 changes: 28 additions & 11 deletions core/shared/src/test/scala/cats/effect/Generators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package cats
package effect

import org.scalacheck._

import scala.util.Either

object Generators {
import Arbitrary._

implicit def arbIO[A: Arbitrary: Cogen]: Arbitrary[IO[A]] = Arbitrary(Gen.delay(genIO[A]))
implicit def arbIO[A: Arbitrary: Cogen]: Arbitrary[IO[A]] =
Arbitrary(Gen.delay(genIO[A]))

def genIO[A: Arbitrary: Cogen]: Gen[IO[A]] = {
Gen.frequency(
Expand All @@ -36,19 +36,36 @@ object Generators {
10 -> genFlatMap[A])
}

def genPure[A: Arbitrary]: Gen[IO[A]] = arbitrary[A].map(IO.pure(_))
def genSyncIO[A: Arbitrary: Cogen]: Gen[IO[A]] = {
Gen.frequency(
5 -> genPure[A],
5 -> genApply[A],
1 -> genFail[A],
5 -> genBindSuspend[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(_))
def genApply[A: Arbitrary]: Gen[IO[A]] =
arbitrary[A].map(IO.apply(_))

def genFail[A]: Gen[IO[A]] = arbitrary[Throwable].map(IO.raiseError(_))
def genFail[A]: Gen[IO[A]] =
arbitrary[Throwable].map(IO.raiseError)

def genAsync[A: Arbitrary]: Gen[IO[A]] = arbitrary[(Either[Throwable, A] => Unit) => Unit].map(IO.async(_))
def genAsync[A: Arbitrary]: Gen[IO[A]] =
arbitrary[(Either[Throwable, A] => Unit) => Unit].map(IO.async)

def genNestedAsync[A: Arbitrary: Cogen]: Gen[IO[A]] =
arbitrary[(Either[Throwable, IO[A]] => Unit) => Unit].map(k => IO.async(k).flatMap(x => x))
arbitrary[(Either[Throwable, IO[A]] => Unit) => Unit]
.map(k => IO.async(k).flatMap(x => x))

def genBindSuspend[A: Arbitrary: Cogen]: Gen[IO[A]] =
arbitrary[A].map(IO.apply(_).flatMap(IO.pure))

def genFlatMap[A: Arbitrary: Cogen]: Gen[IO[A]] = for {
ioa <- arbitrary[IO[A]]
f <- arbitrary[A => IO[A]]
} yield ioa.flatMap(f)
def genFlatMap[A: Arbitrary: Cogen]: Gen[IO[A]] =
for {
ioa <- arbitrary[IO[A]]
f <- arbitrary[A => IO[A]]
} yield ioa.flatMap(f)
}
37 changes: 35 additions & 2 deletions laws/shared/src/test/scala/cats/effect/IOTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,47 @@ class IOTests extends BaseTestsSuite {
val f = io.unsafeToFuture(); ec.tick()
f.value shouldEqual Some(Success(100))
}

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(genSyncIO[Int])

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 =>
ec.execute(new Runnable {
def run(): Unit =
cb(Right(a))
})
}

val f = repeatedTransformLoop(10000, async(99)).unsafeToFuture()
ec.tick()
f.value shouldEqual Some(Success(99))
}
}

object IOTests {
/** Implementation for testing default methods. */
val ioEffectDefaults = new Effect[IO] {
private val ref = implicitly[Effect[IO]]

def async[A](k: ((Either[Throwable, A]) => Unit) => Unit): IO[A] =
ref.async(k)
def raiseError[A](e: Throwable): IO[A] =
Expand All @@ -373,7 +408,5 @@ object IOTests {
ref.runAsync(fa)(cb)
def suspend[A](thunk: =>IO[A]): IO[A] =
ref.suspend(thunk)
def liftIO[A](ioa: IO[A]): IO[A] =
ref.liftIO(ioa)
}
}