final class SyncIO[+A] extends AnyRef
A pure abstraction representing the intention to perform a side effect, where the result of that side effect is obtained synchronously.
SyncIO
is similar to IO, but does not support asynchronous
computations. Consequently, a SyncIO
can be run synchronously
to obtain a result via unsafeRunSync
. This is unlike
IO#unsafeRunSync
, which cannot be safely called in general --
doing so on the JVM blocks the calling thread while the
async part of the computation is run and doing so on Scala.js
throws an exception upon encountering an async boundary.
- Source
- SyncIO.scala
- Alphabetic
- By Inheritance
- SyncIO
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
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
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def attempt: SyncIO[Either[Throwable, A]]
Materializes any sequenced exceptions into value space, where they may be handled.
Materializes any sequenced exceptions into value space, where they may be handled.
This is analogous to the
catch
clause intry
/catch
, being the inverse ofSyncIO.raiseError
. Thus:SyncIO.raiseError(ex).attempt.unsafeRunSync === Left(ex)
- See also
- def bracket[B](use: (A) => SyncIO[B])(release: (A) => SyncIO[Unit]): SyncIO[B]
Returns a
SyncIO
action that treats the source task as the acquisition of a resource, which is then exploited by theuse
function and thenreleased
.Returns a
SyncIO
action that treats the source task as the acquisition of a resource, which is then exploited by theuse
function and thenreleased
.The
bracket
operation is the equivalent of thetry {} catch {} finally {}
statements from mainstream languages.The
bracket
operation installs the necessary exception handler to release the resource in the event of an exception being raised during the computation.If an exception is raised, then
bracket
will re-raise the exception after performing therelease
.NOTE on error handling: one big difference versus
try/finally
statements is that, in case both therelease
function and theuse
function throws, the error raised byuse
gets signaled.For example:
SyncIO("resource").bracket { _ => // use SyncIO.raiseError(new RuntimeException("Foo")) } { _ => // release SyncIO.raiseError(new RuntimeException("Bar")) }
In this case the error signaled downstream is
"Foo"
, while the"Bar"
error gets reported. This is consistent with the behavior of Haskell'sbracket
operation and NOT withtry {} finally {}
from Scala, Java or JavaScript.- use
is a function that evaluates the resource yielded by the source, yielding a result that will get generated by the task returned by this
bracket
function- release
is a function that gets called after
use
terminates, either normally or in error, or if it gets canceled, receiving as input the resource that needs to be released
- See also
- def bracketCase[B](use: (A) => SyncIO[B])(release: (A, ExitCase[Throwable]) => SyncIO[Unit]): SyncIO[B]
Returns a new
SyncIO
task that treats the source task as the acquisition of a resource, which is then exploited by theuse
function and thenreleased
, with the possibility of distinguishing between normal termination and failure, such that an appropriate release of resources can be executed.Returns a new
SyncIO
task that treats the source task as the acquisition of a resource, which is then exploited by theuse
function and thenreleased
, with the possibility of distinguishing between normal termination and failure, such that an appropriate release of resources can be executed.The
bracketCase
operation is the equivalent oftry {} catch {} finally {}
statements from mainstream languages when used for the acquisition and release of resources.The
bracketCase
operation installs the necessary exception handler to release the resource in the event of an exception being raised during the computation.In comparison with the simpler bracket version, this one allows the caller to differentiate between normal termination and termination in error. Note
SyncIO
does not support cancelation so that exit case should be ignored.- use
is a function that evaluates the resource yielded by the source, yielding a result that will get generated by this function on evaluation
- release
is a function that gets called after
use
terminates, either normally or in error, receiving as input the resource that needs that needs release, along with the result ofuse
(error or successful result)
- See also
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def flatMap[B](f: (A) => SyncIO[B]): SyncIO[B]
Monadic bind on
SyncIO
, used for sequentially composing twoSyncIO
actions, where the value produced by the firstSyncIO
is passed as input to a function producing the secondSyncIO
action.Monadic bind on
SyncIO
, used for sequentially composing twoSyncIO
actions, where the value produced by the firstSyncIO
is passed as input to a function producing the secondSyncIO
action.Due to this operation's signature,
flatMap
forces a data dependency between twoSyncIO
actions, thus ensuring sequencing (e.g. one action to be executed before another one).Any exceptions thrown within the function will be caught and sequenced in to the result
SyncIO[B]
. - final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def guarantee(finalizer: SyncIO[Unit]): SyncIO[A]
Executes the given
finalizer
when the source is finished, either in success or in error.Executes the given
finalizer
when the source is finished, either in success or in error.This variant of guaranteeCase evaluates the given
finalizer
regardless of how the source gets terminated:- normal completion
- completion in error
This equivalence always holds:
io.guarantee(f) <-> IO.unit.bracket(_ => io)(_ => f)
As best practice, it's not a good idea to release resources via
guaranteeCase
in polymorphic code. Prefer bracket for the acquisition and release of resources.- See also
guaranteeCase for the version that can discriminate between termination conditions
bracket for the more general operation
- def guaranteeCase(finalizer: (ExitCase[Throwable]) => SyncIO[Unit]): SyncIO[A]
Executes the given
finalizer
when the source is finished, either in success or in error, allowing for differentiating between exit conditions.Executes the given
finalizer
when the source is finished, either in success or in error, allowing for differentiating between exit conditions.This variant of guarantee injects an ExitCase in the provided function, allowing one to make a difference between:
- normal completion
- completion in error
This equivalence always holds:
io.guaranteeCase(f) <-> IO.unit.bracketCase(_ => io)((_, e) => f(e))
As best practice, it's not a good idea to release resources via
guaranteeCase
in polymorphic code. Prefer bracketCase for the acquisition and release of resources.- See also
guarantee for the simpler version
bracketCase for the more general operation
- def handleErrorWith[AA >: A](f: (Throwable) => SyncIO[AA]): SyncIO[AA]
Handle any error, potentially recovering from it, by mapping it to another
SyncIO
value.Handle any error, potentially recovering from it, by mapping it to another
SyncIO
value.Implements
ApplicativeError.handleErrorWith
. - def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def map[B](f: (A) => B): SyncIO[B]
Functor map on
SyncIO
.Functor map on
SyncIO
. Given a mapping function, it transforms the value produced by the source, while keeping theSyncIO
context.Any exceptions thrown within the function will be caught and sequenced in to the result
SyncIO[B]
. - final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def redeem[B](recover: (Throwable) => B, map: (A) => B): SyncIO[B]
Returns a new value that transforms the result of the source, given the
recover
ormap
functions, which get executed depending on whether the result ends in error or if it is successful.Returns a new value that transforms the result of the source, given the
recover
ormap
functions, which get executed depending on whether the result ends in error or if it is successful.This is an optimization on usage of attempt and map, this equivalence being true:
io.redeem(recover, map) <-> io.attempt.map(_.fold(recover, map))
Usage of
redeem
subsumeshandleError
because:io.redeem(fe, id) <-> io.handleError(fe)
- recover
is a function used for error recover in case the source ends in error
- map
is a function used for mapping the result of the source in case it ends in success
- def redeemWith[B](recover: (Throwable) => SyncIO[B], bind: (A) => SyncIO[B]): SyncIO[B]
Returns a new value that transforms the result of the source, given the
recover
orbind
functions, which get executed depending on whether the result ends in error or if it is successful.Returns a new value that transforms the result of the source, given the
recover
orbind
functions, which get executed depending on whether the result ends in error or if it is successful.This is an optimization on usage of attempt and flatMap, this equivalence being available:
io.redeemWith(recover, bind) <-> io.attempt.flatMap(_.fold(recover, bind))
Usage of
redeemWith
subsumeshandleErrorWith
because:io.redeemWith(fe, F.pure) <-> io.handleErrorWith(fe)
Usage of
redeemWith
also subsumes flatMap because:io.redeemWith(F.raiseError, fs) <-> io.flatMap(fs)
- recover
is the function that gets called to recover the source in case of error
- bind
is the function that gets to transform the source in case of success
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def to[F[_]](implicit F: LiftIO[F]): F[A]
Converts the source
IO
into anyF
type that implements the LiftIO type class. - def toIO: IO[A]
- def toString(): String
- Definition Classes
- SyncIO → AnyRef → Any
- def unsafeRunSync(): A
Produces the result by running the encapsulated effects as impure side effects.
Produces the result by running the encapsulated effects as impure side effects.
Any exceptions raised within the effect will be re-thrown during evaluation.
As the name says, this is an UNSAFE function as it is impure and performs side effects and throws exceptions. You should ideally only call this function *once*, at the very end of your program.
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated
This is the API documentation for the cats-effect library.
See the cats.effect package for a quick overview.
Links
Canonical documentation links:
Related Cats links (the core):