Given a Resource
, possibly built by composing multiple
Resource
s monadically, returns the acquired resource, as well
as an action that runs all the finalizers for releasing it.
Given a Resource
, possibly built by composing multiple
Resource
s monadically, returns the acquired resource, as well
as an action that runs all the finalizers for releasing it.
If the outer F
fails or is interrupted, allocated
guarantees
that the finalizers will be called. However, if the outer F
succeeds, it's up to the user to ensure the returned F[Unit]
is called once A
needs to be released. If the returned
F[Unit]
is not called, the finalizers will not be run.
For this reason, this is an advanced and potentially unsafe api
which can cause a resource leak if not used correctly, please
prefer use as the standard way of running a Resource
program.
Use cases include interacting with side-effectful apis that
expect separate acquire and release actions (like the before
and after
methods of many test frameworks), or complex library
code that needs to modify or move the finalizer for an existing
resource.
Implementation for the flatMap
operation, as described via the
cats.Monad
type class.
Allocates a resource and supplies it to the given function.
Allocates a resource and supplies it to the given function. The
resource is released as soon as the resulting F[B]
is
completed, whether normally or as a raised error.
the function to apply to the allocated resource
the result of applying [F] to
The
Resource
is a data structure that captures the effectful allocation of a resource, along with its finalizer.This can be used to wrap expensive resources. Example:
Usage is done via use and note that resource usage nests, because its implementation is specified in terms of Bracket:
Resource
forms aMonadError
on the resource type when the effect type has acats.MonadError
instance. Nested resources are released in reverse order of acquisition. Outer resources are released even if an inner use or release fails.On evaluation the above prints:
A
Resource
is nothing more than a data structure, an ADT, described by the following node types and that can be interpretted if needed:Normally users don't need to care about these node types, unless conversions from
Resource
into something else is needed (e.g. conversion fromResource
into a streaming data type).the effect type in which the resource is allocated and released
the type of resource