package zio
- Alphabetic
- By Inheritance
- zio
- EitherCompat
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
trait
App extends RTS
The entry point for a purely-functional application on the JVM.
The entry point for a purely-functional application on the JVM.
import java.io.IOException import scalaz.zio.{App, IO} import scalaz.zio.console._ object MyApp extends App { final def run(args: List[String]): IO[Nothing, ExitStatus] = myAppLogic.attempt.map(_.fold(_ => 1, _ => 0)).map(ExitStatus.ExitNow(_)) def myAppLogic: IO[IOException, Unit] = for { _ <- putStrLn("Hello! What is your name?") n <- getStrLn _ <- putStrLn("Hello, " + n + ", good to meet you!") } yield () }
-
sealed abstract
class
Async[+E, +A] extends Product with Serializable
The
Async
class describes the return value of an asynchronous effect that is imported into anIO
value.The
Async
class describes the return value of an asynchronous effect that is imported into anIO
value. Asynchronous effects can return values synchronously via returningnow
, or promise to invoke a callback via returninglater
. - type Canceler = IO[Nothing, _]
-
sealed
trait
Chunk[+A] extends AnyRef
A
Chunk[A]
represents a chunk of values of typeA
.A
Chunk[A]
represents a chunk of values of typeA
. Chunks are designed are usually backed by arrays, but expose a purely functional, safe interface to the underlying elements, and they become lazy on operations that would be costly with arrays, such as repeated concatenation. - trait Clock extends Serializable
- trait EitherCompat extends AnyRef
-
sealed abstract
class
Exit[+E, +A] extends Product with Serializable
An
Exit[E, A]
describes the result of executing anIO
value.An
Exit[E, A]
describes the result of executing anIO
value. The result is either succeeded with a valueA
, or failed with aCause[E]
. -
trait
Fiber[+E, +A] extends AnyRef
A fiber is a lightweight thread of execution that never consumes more than a whole thread (but may consume much less, depending on contention).
A fiber is a lightweight thread of execution that never consumes more than a whole thread (but may consume much less, depending on contention). Fibers are spawned by forking
IO
actions, which, conceptually at least, runs them concurrently with the parentIO
action.Fibers can be joined, yielding their result other fibers, or interrupted, which terminates the fiber with a runtime error.
Fork-Join Identity: fork >=> join = id
for { fiber1 <- io1.fork fiber2 <- io2.fork _ <- fiber1.interrupt(e) a <- fiber2.join } yield a
- final case class FiberFailure(cause: Cause[Any]) extends Throwable with Product with Serializable
- type FiberId = Long
-
final
class
FiberLocal[A] extends Serializable
A container for fiber-local storage.
A container for fiber-local storage. It is the pure equivalent to Java's
ThreadLocal
on a fiber architecture. -
sealed abstract
class
FunctionIO[+E, -A, +B] extends Serializable
A
FunctionIO[E, A, B]
is an effectful function fromA
toB
, which might fail with anE
.A
FunctionIO[E, A, B]
is an effectful function fromA
toB
, which might fail with anE
.This is the moral equivalent of
A => IO[E, B]
, and, indeed,FunctionIO
extends this function type, and can be used in the same way.The main advantage to using
FunctionIO
is that it provides you a means of importing an impure functionA => B
intoFunctionIO[E, A, B]
, without actually wrapping the result of the function in anIO
value.This allows the implementation to aggressively fuse operations on impure functions, which in turn can result in significantly higher-performance and far less heap utilization than equivalent approaches modeled with
IO
.The implementation allows you to lift functions from
A => IO[E, B]
into aFunctionIO[E, A, B]
. Such functions cannot be optimized, but will be handled correctly and can work in conjunction with optimized (fused)FunctionIO
.Those interested in learning more about modeling effects with
FunctionIO
are encouraged to read John Hughes paper on the subject: Generalizing Monads to Arrows (www.cse.chalmers.se/~rjmh/Papers/arrows.pdf). The implementation in this file contains many of the same combinators as Hughes implementation.A word of warning: while even very complex code can be expressed in
FunctionIO
, there is a point of diminishing return. If you find yourself using deeply nested tuples to propagate information forward, it may be no faster than usingIO
.Given the following two
FunctionIO
:val readLine = FunctionIO.impureVoid((_ : Unit) => scala.Console.readLine()) val printLine = FunctionIO.impureVoid((line: String) => println(line))
Then the following two programs are equivalent:
// Program 1 val program1: IO[Nothing, Unit] = for { name <- getStrLn _ <- putStrLn("Hello, " + name) } yield ()) // Program 2 val program2: IO[Nothing, Unit] = (readLine >>> FunctionIO.lift("Hello, " + _) >>> printLine)(())
Similarly, the following two programs are equivalent:
// Program 1 val program1: IO[Nothing, Unit] = for { line1 <- getStrLn line2 <- getStrLn _ <- putStrLn("You wrote: " + line1 + ", " + line2) } yield ()) // Program 2 val program2: IO[Nothing, Unit] = (readLine.zipWith(readLine)("You wrote: " + _ + ", " + _) >>> printLine)(())
In both of these examples, the
FunctionIO
program is faster because it is able to perform fusion of effectful functions. -
sealed abstract
class
IO[+E, +A] extends Serializable
An
IO[E, A]
("Eye-Oh of Eeh Aye") is an immutable data structure that describes an effectful action that may fail with anE
, run forever, or produce a singleA
at some point in the future.An
IO[E, A]
("Eye-Oh of Eeh Aye") is an immutable data structure that describes an effectful action that may fail with anE
, run forever, or produce a singleA
at some point in the future.Conceptually, this structure is equivalent to
EitherT[F, E, A]
for some infallible effect monadF
, but because monad transformers perform poorly in Scala, this structure bakes in theEitherT
without runtime overhead.IO
values are ordinary immutable values, and may be used like any other values in purely functional code. BecauseIO
values just *describe* effects, which must be interpreted by a separate runtime system, they are entirely pure and do not violate referential transparency.IO
values can efficiently describe the following classes of effects:- Pure Values —
IO.point
- Synchronous Effects —
IO.sync
- Asynchronous Effects —
IO.async
- Concurrent Effects —
io.fork
- Resource Effects —
io.bracket
The concurrency model is based on fibers, a user-land lightweight thread, which permit cooperative multitasking, fine-grained interruption, and very high performance with large numbers of concurrently executing fibers.
IO
values compose with otherIO
values in a variety of ways to build complex, rich, interactive applications. See the methods onIO
for more details about how to composeIO
values.In order to integrate with Scala,
IO
values must be interpreted into the Scala runtime. This process of interpretation executes the effects described by a given immutableIO
value. For more information on interpretingIO
values, see the default interpreter inRTS
or the safe main function inApp
. - Pure Values —
-
final
case class
Managed[+E, +R](reserve: IO[E, Reservation[E, R]]) extends Product with Serializable
A
Managed[E, R]
is a managed resource of typeR
, which may be used by invoking theuse
method of the resource.A
Managed[E, R]
is a managed resource of typeR
, which may be used by invoking theuse
method of the resource. The resource will be automatically acquired before the resource is used, and automatically released after the resource is used.Resources do not survive the scope of
use
, meaning that if you attempt to capture the resource, leak it fromuse
, and then use it after the resource has been consumed, the resource will not be valid anymore and may fail with some checked error, as per the type of the functions provided by the resource. -
final
class
Promise[E, A] extends AnyVal
A promise represents an asynchronous variable that can be set exactly once, with the ability for an arbitrary number of fibers to suspend (by calling
get
) and automatically resume when the variable is set.A promise represents an asynchronous variable that can be set exactly once, with the ability for an arbitrary number of fibers to suspend (by calling
get
) and automatically resume when the variable is set.Promises can be used for building primitive actions whose completions require the coordinated action of multiple fibers, and for building higher-level concurrent or asynchronous structures.
for { promise <- Promise.make[Nothing, Int] _ <- promise.complete(42).delay(1.second).fork value <- promise.get // Resumes when forked fiber completes promise } yield value
-
class
Queue[A] extends Serializable
A
Queue[A]
is a lightweight, asynchronous queue for values of typeA
. - trait RTS extends AnyRef
-
final
class
Ref[A] extends AnyVal with Serializable
A mutable atomic reference for the
IO
monad.A mutable atomic reference for the
IO
monad. This is theIO
equivalent of a volatilevar
, augmented with atomic operations, which make it useful as a reasonably efficient (if low-level) concurrency primitive.for { ref <- Ref(2) v <- ref.update(_ + 3) _ <- putStrLn("Value = " + v) // Value = 5 } yield ()
-
final
class
RefM[A] extends Serializable
A mutable atomic reference for the
IO
monad.A mutable atomic reference for the
IO
monad. This is theIO
equivalent of a volatilevar
, augmented with atomic effectful operations, which make it useful as a reasonably efficient (if low-level) concurrency primitive.Unlike
Ref
,RefM
allows effects in atomic operations, which makes the structure slower but more powerful thanRef
.for { ref <- RefM(2) v <- ref.update(_ + putStrLn("Hello World!").attempt.void *> IO.succeed(3)) _ <- putStrLn("Value = " + v) // Value = 5 } yield ()
-
trait
Schedule[-A, +B] extends Serializable
Defines a stateful, possibly effectful, recurring schedule of actions.
Defines a stateful, possibly effectful, recurring schedule of actions.
A
Schedule[A, B]
consumesA
values, and based on the inputs and the internal state, decides whether to continue or halt. Every decision is accompanied by a (possibly zero) delay, and an output value of typeB
.Schedules compose in each of the following ways:
1. Intersection, using the
&&
operator, which requires that both schedules continue, using the longer of the two durations. 2. Union, using the||
operator, which requires that only one schedule continues, using the shorter of the two durations. 3. Sequence, using the<||>
operator, which runs the first schedule until it ends, and then switches over to the second schedule.Schedule[A, B]
forms a profunctor on[A, B]
, an applicative functor onB
, and a monoid, allowing rich composition of different schedules. - final class Semaphore extends Serializable
-
trait
ZIO[F[_, _]] extends AnyRef
A type class that allows people to use ZIO-specific code with other effect monads, so long as they can lift an arbitrary
IO
computation into their type.A type class that allows people to use ZIO-specific code with other effect monads, so long as they can lift an arbitrary
IO
computation into their type. See theinterop
modules for instances of this type class for other effect types.
Value Members
- object Async extends Serializable
-
object
BuildInfo extends Product with Serializable
This object was generated by sbt-buildinfo.
- object Chunk
- object Clock extends Serializable
- object Exit extends Serializable
- object Fiber
- object FiberLocal extends Serializable
- object FunctionIO extends Serializable
- object IO extends Serializable
- object Managed extends Serializable
- object Promise
- object Queue extends Serializable
- object Ref extends Serializable
- object RefM extends Serializable
- object Schedule extends Serializable
- object Semaphore extends Serializable
- object ZIO extends Serializable