Pure
Value members
Inherited methods
Implementation for the
flatMap
operation, as described via thecats.Monad
type class.- Inhertied from
- Resource
Creates a FunctionK that, when applied, will allocate the resource and use it to run the given Kleisli.
- Inhertied from
- Resource
def mapK[G <: ([_$11] =>> Any)](f: FunctionK[F, G])(F: MonadCancel[F, ], G: MonadCancel[G, ]): Resource[G, A]
Given a natural transformation from
Resource from effect
The F and G constraint can also be satisfied by requiring a
MonadCancelThrow[F] and MonadCancelThrow[G] .
F
to G
, transforms thisResource from effect
F
to effect G
.The F and G constraint can also be satisfied by requiring a
MonadCancelThrow[F] and MonadCancelThrow[G] .
- Inhertied from
- Resource
Allocates a resource with a non-terminating use action.
Useful to run programs that are expressed entirely in
Useful to run programs that are expressed entirely in
Resource
.The finalisers run when the resulting program fails or gets interrupted.
- Inhertied from
- Resource
Creates a FunctionK that can run
gb
within a resource, which is then closed once gb
terminates, fails or gets interrupted- Inhertied from
- Resource
Applies an effectful transformation to the allocated resource. Like a
flatMap
on F[A]
while maintaining the resource context- Inhertied from
- Resource
Allocates a resource and supplies it to the given function.
The resource is released as soon as the resulting
completed, whether normally or as a raised error.
The resource is released as soon as the resulting
F[B]
iscompleted, whether normally or as a raised error.
- Value Params
- f
-
the function to apply to the allocated resource
- Returns
-
the result of applying [F] to
- Inhertied from
- Resource
Given a mapping function, transforms the resource provided by
this Resource.
this Resource.
This is the standard
Functor.map
.- Inhertied from
- Resource
Runs
run even if resource acquisition fails or is canceled.
finalizer
when this resource is closed. Unlike the release action passed to Resource.make
, this willrun even if resource acquisition fails or is canceled.
- Inhertied from
- Resource
Acquires the resource, runs
gb
and closes the resource once gb
terminates, fails or gets interrupted- Inhertied from
- Resource
Allocates two resources concurrently, and combines their results in a tuple.
The finalizers for the two resources are also run concurrently with each other,
but within each of the two resources, nested finalizers are run in the usual
reverse order of acquisition.
but within each of the two resources, nested finalizers are run in the usual
reverse order of acquisition.
Note that
that offers more convenient access to the same functionality as
Resource
also comes with a cats.Parallel
instancethat offers more convenient access to the same functionality as
parZip
, for example via parMapN
:{{{
def mkResource(name: String) = {
val acquire =
IO(scala.util.Random.nextInt(1000).millis).flatMap(IO.sleep) *>
IO(println(s"Acquiring $$name")).as(name)
def mkResource(name: String) = {
val acquire =
IO(scala.util.Random.nextInt(1000).millis).flatMap(IO.sleep) *>
IO(println(s"Acquiring $$name")).as(name)
val release = IO(println(s"Releasing $$name"))
Resource.make(acquire)(release)
}
Resource.make(acquire)(release)
}
val r = (mkResource("one"), mkResource("two"))
.parMapN((s1, s2) => s"I have $s1 and $s2")
.use(msg => IO(println(msg)))
}}}
.parMapN((s1, s2) => s"I have $s1 and $s2")
.use(msg => IO(println(msg)))
}}}
- Inhertied from
- Resource
Applies an effectful transformation to the allocated resource. Like a
flatTap
on F[A]
while maintaining the resource context- Inhertied from
- Resource
Given a
as an action that runs all the finalizers for releasing it.
Resource
, possibly built by composing multipleResource
s monadically, returns the acquired resource, as wellas an action that runs all the finalizers for releasing it.
If the outer
that the finalizers will be called. However, if the outer
succeeds, it's up to the user to ensure the returned
is called once
F
fails or is interrupted, allocated
guaranteesthat 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 returnedF[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
program.
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
and
code that needs to modify or move the finalizer for an existing
resource.
expect separate acquire and release actions (like the
before
and
after
methods of many test frameworks), or complex librarycode that needs to modify or move the finalizer for an existing
resource.
- Inhertied from
- Resource