-
Notifications
You must be signed in to change notification settings - Fork 508
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
Changes from 6 commits
be41c03
ef3a870
af72b60
f7e1d41
3660c5e
8dc3cc8
1822867
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 This is exactly what I had in mind. |
||
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)" | ||
|
@@ -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]] { | ||
|
@@ -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] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,12 +351,48 @@ 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[IO[Int]] = | ||
Arbitrary(Gen.delay(genSyncIO[Int])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's the same thing. Depending on There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 => | ||
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] = | ||
|
@@ -373,7 +409,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) | ||
} | ||
} |
There was a problem hiding this comment.
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.