object IO extends IOInstances
- Source
- IO.scala
- Alphabetic
- By Inheritance
- IO
- IOInstances
- IOLowPriorityInstances
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
apply[A](body: ⇒ A): IO[A]
Suspends a synchronous side effect in
IO
.Suspends a synchronous side effect in
IO
.Any exceptions thrown by the effect will be caught and sequenced into the
IO
. -
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
async[A](k: ((Either[Throwable, A]) ⇒ Unit) ⇒ Unit): IO[A]
Suspends an asynchronous side effect in
IO
.Suspends an asynchronous side effect in
IO
.The given function will be invoked during evaluation of the
IO
to "schedule" the asynchronous callback, where the callback is the parameter passed to that function. Only the first invocation of the callback will be effective! All subsequent invocations will be silently dropped.As a quick example, you can use this function to perform a parallel computation given an
ExecutorService
:def fork[A](body: => A)(implicit E: ExecutorService): IO[A] = { IO async { cb => E.execute(new Runnable { def run() = try cb(Right(body)) catch { case NonFatal(t) => cb(Left(t)) } }) } }
The
fork
function will do exactly what it sounds like: take a thunk and anExecutorService
and run that thunk on the thread pool. Or rather, it will produce anIO
which will do those things when run; it does *not* schedule the thunk until the resultingIO
is run! Note that there is no thread blocking in this implementation; the resultingIO
encapsulates the callback in a pure and monadic fashion without using threads.This function can be thought of as a safer, lexically-constrained version of
Promise
, whereIO
is like a safer, lazy version ofFuture
. -
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
eval[A](effect: Eval[A]): IO[A]
Lifts an
Eval
intoIO
.Lifts an
Eval
intoIO
.This function will preserve the evaluation semantics of any actions that are lifted into the pure
IO
. EagerEval
instances will be converted into thunk-lessIO
(i.e. eagerIO
), while lazy eval and memoized will be executed as such. -
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
fromFuture[A](f: Eval[Future[A]])(implicit ec: ExecutionContext): IO[A]
Constructs an
IO
which evaluates the givenFuture
and produces the result (or failure).Constructs an
IO
which evaluates the givenFuture
and produces the result (or failure).Because
Future
eagerly evaluates, as well as because it memoizes, this function takes its parameter as acats.Eval
, which could be lazily evaluated. If this laziness is appropriately threaded back to the definition site of theFuture
, it ensures that the computation is fully managed byIO
and thus referentially transparent.The
cats.Eval
type allows fine grained control of how the passedFuture
reference gets evaluated. Example:import cats.Eval.{always, later, now} // Lazy evaluation, equivalent with by-name params IO.fromFuture(always(f)) // Memoized, lazy evaluation, equivalent with lazy val IO.fromFuture(later(f)) // Eager evaluation IO.fromFuture(now(f))
Note that the continuation of the computation resulting from a
Future
will run on the future's thread pool. There is no thread shifting here; theExecutionContext
is solely for the benefit of theFuture
.Roughly speaking, the following identities hold:
IO.fromFuture(always(f)).unsafeToFuture() === f // true-ish (except for memoization) IO.fromFuture(always(ioa.unsafeToFuture())) === ioa // true
- See also
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
implicit
val
ioEffect: Effect[IO]
- Definition Classes
- IOInstances
-
implicit
def
ioMonoid[A](implicit arg0: Monoid[A]): Monoid[IO[A]]
- Definition Classes
- IOInstances
-
implicit
def
ioSemigroup[A](implicit arg0: Semigroup[A]): Semigroup[IO[A]]
- Definition Classes
- IOLowPriorityInstances
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
def
pure[A](a: A): IO[A]
Suspends a pure value in
IO
.Suspends a pure value in
IO
.This should only be used if the value in question has "already" been computed! In other words, something like
IO.pure(readLine)
is most definitely not the right thing to do! However,IO.pure(42)
is correct and will be more efficient (when evaluated) thanIO(42)
, due to avoiding the allocation of extra thunks. -
def
raiseError[A](e: Throwable): IO[A]
Constructs an
IO
which sequences the specified exception.Constructs an
IO
which sequences the specified exception.If this
IO
is run usingunsafeRunSync
orunsafeRunTimed
, the exception will be thrown. This exception can be "caught" (or rather, materialized into value-space) using theattempt
method.- See also
-
def
suspend[A](thunk: ⇒ IO[A]): IO[A]
Suspends a synchronous side effect which produces an
IO
inIO
.Suspends a synchronous side effect which produces an
IO
inIO
.This is useful for trampolining (i.e. when the side effect is conceptually the allocation of a stack frame). Any exceptions thrown by the side effect will be caught and sequenced into the
IO
. -
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
val
unit: IO[Unit]
Alias for
IO.pure(())
. -
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )