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.
Constructing SyncIO values
import cats.effect.SyncIO // import cats.effect.SyncIO def putStrLn(str: String): SyncIO[Unit] = SyncIO(println(str)) // putStrLn: (str: String)cats.effect.SyncIO[Unit] SyncIO.pure("Cats!").flatMap(putStrLn).unsafeRunSync // Cats!
unit, equivalent to the same operations defined in
IO but with synchronicity guarantees.
Interoperation with Eval and IO
SyncIO defines an
eval method in its companion object to lift any
import cats.Eval // import cats.Eval val eval = Eval.now("hey!") // eval: cats.Eval[String] = Now(hey!) SyncIO.eval(eval).unsafeRunSync // res1: String = hey!
SyncIO also defines a
to[F] method at the class level to lift your value into any
F with a
LiftIO instance available.
import cats.effect.IO // import cats.effect.IO val ioa: SyncIO[Unit] = SyncIO(println("Hello world!")) // ioa: cats.effect.SyncIO[Unit] = SyncIO$271806747 val iob: IO[Unit] = ioa.to[IO] // iob: cats.effect.IO[Unit] = IO$741363930 iob.unsafeRunAsync(_ => ()) // Hello world!