4 stars based on
A pure abstraction representing the intention to perform a side effect, where the result source io that side effect may be obtained synchronously via return or asynchronously via callback.
IO values are pure, immutable values and thus preserve referential transparency, being usable in functional programming. An IO is a data structure that represents just a description of a side effectful computation. Effects described via this abstraction are not evaluated until the "end of the world", which is to say, when one of the "unsafe" methods are used.
Effectful results are not memoized, meaning that memory overhead is minimal and no leaksand also that a single effect may be run multiple times in a referentially-transparent manner. The above will print "hey! IO is trampolined in its flatMap evaluation. This means that you can safely call flatMap in a recursive function of arbitrary depth, without fear of blowing the stack. Materializes any sequenced exceptions into value space, where they may be handled.
Returns an IO action that treats the source task as the acquisition of a resource, which is then exploited by the use function source io then released. The source io operation installs the necessary exception handler to release the resource in the event of an exception being raised during the computation, or in case of cancelation.
If an exception source io raised, then bracket will re-raise the exception after performing the release. Source io the resulting task gets cancelled, then bracket will still perform the releasebut the yielded task will be source io equivalent with IO. Note that source io case of cancelation the underlying implementation cannot guarantee that the computation described by use doesn't end up executed concurrently with the computation from release. In the example above that ugly Java loop might end up reading from a BufferedReader that is already closed due to the task being cancelled, thus source io an error in the background with nowhere to source io signaled.
In this particular example, given that we are just reading from a file, it doesn't matter. For those cases you might want to do synchronization source io. NOTE on error handling: In this case the error signaled downstream is "Foo"while the "Bar" error gets reported. Returns a new IO task that treats the source task as the acquisition of a resource, which is then exploited by the use function and then releasedwith the possibility of distinguishing between normal termination and cancelation, such that an appropriate release of resources can be executed.
The bracketCase operation installs the necessary exception handler to release the resource in the event of an exception being raised during the computation, or in source io of cancelation. In comparison with source io simpler bracket version, this one allows the caller to differentiate between normal termination, termination in error and cancelation via an ExitCase parameter.
Monadic bind on Source ioused for sequentially composing two IO actions, where the value produced by the first IO is passed as input to a function producing the second IO action. Due source io this operation's signature, flatMap forces a data dependency between source io IO actions, thus ensuring sequencing e. Any exceptions thrown within the function will be caught and sequenced into the IObecause due to the nature of asynchronous processes, without catching and handling exceptions, failures would be completely silent and IO references would never terminate on evaluation.
Functor map on IO. Given a source io functions, it transforms the value produced by the source, while keeping the IO context. Returns a new IO that mirrors the source task for normal termination, but that triggers the given error on cancelation. This onCancelRaiseError operator transforms a task that is non-terminating on cancelation into one that yields an error, thus equivalent with IO. Produces an IO reference that should execute the source on evaluation, without waiting for its result, being the safe analogue to unsafeRunAsync.
This operation is isomorphic to unsafeRunAsync. What it does is to let you describe asynchronous source io with a function that stores off the results of the original IO as a side effect, thus avoiding the source io of impure callbacks source io eager evaluation.
The returned IO is guaranteed to execute immediately, and does source io wait on any async action to complete, source io this is safe to do, even on top of runtimes that cannot block threads e. Produces an IO reference that should execute the source on evaluation, without waiting for its result and return a cancelable token, being the safe analogue to unsafeRunCancelable. This operation is isomorphic to unsafeRunCancelable.
Just like runAsyncthis operation avoids the usage of impure callbacks or source io evaluation. The returned IO boxes an Source io that can be used to cancel the running asynchronous computation if the source can be cancelled. The following code is more or less equivalent with parMap2 source io the behavior on error handling and cancelation:.
Note in such a case usage of parMapN via cats. Parallel is still recommended source io of behavior on error and cancelation — consider in the example above what would happen if the first task finishes in error. In that case the second task doesn't get cancelled, which creates source io potential memory leak. But you can use IO. Makes the source IO uninterruptible such that a Fiber.
Passes the result of the encapsulated effects to the given callback by source io them as impure side effects. Any exceptions raised within the effect will be passed to the callback in the Source io. You should ideally only call this function onceat the very end of your program. Source io the source IOpassing the result source io the encapsulated effects to the given callback.
As the name says, this is an UNSAFE function as it is impure and performs side effects, not to mention blocking, throwing exceptions, and doing other things that are at odds with reasonable software.
Similar source io unsafeRunSyncexcept with a bounded blocking duration when awaiting asynchronous results. Thus, if you pass a limit of 5 seconds to an IO consisting solely of synchronous actions, the evaluation may take considerably longer than 5 seconds! As soon as an async blocking limit is hit, evaluation immediately aborts and None is returned. Please note that this function is intended for testing ; it should never appear in your mainline production code!
It is absolutely not an source io function to use if you want to implement timeouts, or anything similar. If you need that sort of functionality, you should source io using a streaming library like fs2 source io Monix.
This is similar to unsafeRunAsync in that it evaluates the IO as a side effect in a non-blocking fashion, but uses a Future rather than an explicit callback.
This function should really only be used if interoperating with legacy code which uses Scala futures. IO can describe synchronous or asynchronous computations that: Ordering Alphabetic By Inheritance. Hide All Show All. Definition Classes AnyRef Annotations native. Normally tasks that are cancelled become non-terminating. Start execution of the source suspended in the IO context. The following code is more or less equivalent with parMap2 minus the behavior on error handling and cancelation: Any exceptions raised within the effect will be re-thrown during evaluation.
Evaluates the effect and produces the result in a Future. Definition Classes AnyRef Annotations throws Source io Classes AnyRef Annotations native throws