sealed abstract class Resource[F[_], A] extends AnyRef
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:
def open(file: File): Resource[IO, BufferedReader] = Resource(IO { val in = new BufferedReader(new FileReader(file)) (in, IO(in.close())) })
Usage is done via use and note that resource usage nests, because its implementation is specified in terms of Bracket:
open(file1).use { in1 =>
open(file2).use { in2 =>
readFiles(in1, in2)
}
}Resource forms a MonadError on the resource type when the
effect type has a cats.MonadError instance. Nested resources are
released in reverse order of acquisition. Outer resources are
released even if an inner use or release fails.
def mkResource(s: String) = { val acquire = IO(println(s"Acquiring $$s")) *> IO.pure(s) def release(s: String) = IO(println(s"Releasing $$s")) Resource.make(acquire)(release) } val r = for { outer <- mkResource("outer") inner <- mkResource("inner") } yield (outer, inner) r.use { case (a, b) => IO(println(s"Using $$a and $$b")) }
On evaluation the above prints:
Acquiring outer Acquiring inner Using outer and inner Releasing inner Releasing outer
A Resource is nothing more than a data structure, an ADT, described by
the following node types and that can be interpreted 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 from Resource
into a streaming data type).
- F
the effect type in which the resource is allocated and released
- A
the type of resource
- Source
- Resource.scala
- Alphabetic
- By Inheritance
- Resource
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
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
-
def
allocated(implicit F: Bracket[F, Throwable]): F[(A, F[Unit])]
Given a
Resource, possibly built by composing multipleResources 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 multipleResources monadically, returns the acquired resource, as well as an action that runs all the finalizers for releasing it.If the outer
Ffails or is interrupted,allocatedguarantees that the finalizers will be called. However, if the outerFsucceeds, it's up to the user to ensure the returnedF[Unit]is called onceAneeds 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
Resourceprogram.Use cases include interacting with side-effectful apis that expect separate acquire and release actions (like the
beforeandaftermethods of many test frameworks), or complex library code that needs to modify or move the finalizer for an existing resource. -
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
evalMap[B](f: (A) ⇒ F[B])(implicit F: Applicative[F]): Resource[F, B]
Applies an effectful transformation to the allocated resource.
Applies an effectful transformation to the allocated resource. Like a
flatMaponF[A]while maintaining the resource context -
def
evalTap[B](f: (A) ⇒ F[B])(implicit F: Applicative[F]): Resource[F, A]
Applies an effectful transformation to the allocated resource.
Applies an effectful transformation to the allocated resource. Like a
flatTaponF[A]while maintaining the resource context -
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
flatMap[B](f: (A) ⇒ Resource[F, B]): Resource[F, B]
Implementation for the
flatMapoperation, as described via thecats.Monadtype class. -
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
map[B](f: (A) ⇒ B)(implicit F: Applicative[F]): Resource[F, B]
Given a mapping function, transforms the resource provided by this Resource.
Given a mapping function, transforms the resource provided by this Resource.
This is the standard
Functor.map. -
def
mapK[G[_]](f: ~>[F, G])(implicit B: Bracket[F, Throwable], D: Defer[G], G: Applicative[G]): Resource[G, A]
Given a natural transformation from
FtoG, transforms this Resource from effectFto effectG. -
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
def
use[B](f: (A) ⇒ F[B])(implicit F: Bracket[F, Throwable]): F[B]
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.- f
the function to apply to the allocated resource
- returns
the result of applying [F] to
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )

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):