-
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 2 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 => uV} | ||
import scala.concurrent.{ExecutionContext, Future, Promise} | ||
import scala.concurrent.duration._ | ||
import scala.util.{Left, Right} | ||
|
@@ -380,6 +381,20 @@ 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 @uV] = | ||
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. Minor sidebar: I would rather just write out the whole 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. OK, will fix. |
||
F.suspend { | ||
unsafeStep match { | ||
case Pure(a) => F.pure(a) | ||
case RaiseError(ex) => F.raiseError(ex) | ||
case async => | ||
F.async(async.unsafeRunAsync) | ||
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. Should keep threading suspends, rather than just immediately jumping to async. It should be possible to convert an entirely synchronous 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. I'm not sure what you mean. Doesn't From what I've seen that branch is hit only if we have an 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. Yeah I just realized you're using I guess it's sort of an open question whether it's better to collapse bind chains or carry them forward into the structure of the target monad. I was originally thinking of the latter implementation, but the former is possible. Both should be lawful. |
||
} | ||
} | ||
|
||
override def toString = this match { | ||
case Pure(a) => s"IO($a)" | ||
case RaiseError(e) => s"IO(throw $e)" | ||
|
@@ -426,7 +441,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]] { | ||
|
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.