Packages

sealed abstract class Resource[+F[_], +A] extends ResourceLike[F, A]

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
Linear Supertypes
ResourceLike[F, A], AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Resource
  2. ResourceLike
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def allocated[G[x] >: F[x], B >: A](implicit F: BracketThrow[G]): G[(B, G[Unit])]

    Given a Resource, possibly built by composing multiple Resources 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 Resources 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.

    Definition Classes
    ResourceLike
  5. def allocated_[G[x] >: F[x], B >: A](implicit F: BracketThrow[G]): G[(B, G[Unit])]

    Implementation of allocated, which is declared in ResourceLike

    Implementation of allocated, which is declared in ResourceLike

    Attributes
    protected
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  8. def combineK[G[x] >: F[x], B >: A](that: Resource[G, B])(implicit F: Sync[G], K: SemigroupK[G]): Resource[G, B]

    Combines two Resource instances by lifting the behaviour of a SemigroupK instance into the resource context

  9. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  10. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  11. def evalMap[G[x] >: F[x], B](f: (A) => G[B])(implicit F: Applicative[G]): Resource[G, B]

    Applies an effectful transformation to the allocated resource.

    Applies an effectful transformation to the allocated resource. Like a flatMap on F[A] while maintaining the resource context

    Definition Classes
    ResourceLike
  12. def evalMap_[G[x] >: F[x], B](f: (A) => G[B])(implicit F: Applicative[G]): Resource[G, B]

    Implementation of evalMap, which is declared in ResourceLike

    Implementation of evalMap, which is declared in ResourceLike

    Attributes
    protected
  13. def evalTap[G[x] >: F[x], B](f: (A) => G[B])(implicit F: Applicative[G]): Resource[G, A]

    Applies an effectful transformation to the allocated resource.

    Applies an effectful transformation to the allocated resource. Like a flatTap on F[A] while maintaining the resource context

    Definition Classes
    ResourceLike
  14. def evalTap_[G[x] >: F[x], B](f: (A) => G[B])(implicit F: Applicative[G]): Resource[G, A]

    Implementation of evalTap, which is declared in ResourceLike

    Implementation of evalTap, which is declared in ResourceLike

    Attributes
    protected
  15. def flatMap[G[x] >: F[x], B](f: (A) => Resource[G, B]): Resource[G, B]

    Implementation for the flatMap operation, as described via the cats.Monad type class.

    Implementation for the flatMap operation, as described via the cats.Monad type class.

    Definition Classes
    ResourceLike
  16. def flatMap_[G[x] >: F[x], B](f: (A) => Resource[G, B]): Resource[G, B]

    Implementation of flatMap, which is declared in ResourceLike

    Implementation of flatMap, which is declared in ResourceLike

    Attributes
    protected
  17. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  18. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  19. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  20. def map[G[x] >: F[x], B](f: (A) => B)(implicit F: Applicative[G]): Resource[G, 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.

    Definition Classes
    ResourceLike
  21. def mapK[G[x] >: F[x], H[_]](f: ~>[G, H])(implicit D: Defer[H], G: Applicative[H]): Resource[H, A]

    Given a natural transformation from F to G, transforms this Resource from effect F to effect G.

  22. def map_[G[x] >: F[x], B](f: (A) => B)(implicit F: Applicative[G]): Resource[G, B]

    Implementation of map, which is declared in ResourceLike

    Implementation of map, which is declared in ResourceLike

    Attributes
    protected
  23. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  24. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  25. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  26. def onFinalize[G[x] >: F[x]](finalizer: G[Unit])(implicit F: Applicative[G]): Resource[G, A]

    Runs finalizer when this resource is closed.

    Runs finalizer when this resource is closed. Unlike the release action passed to Resource.make, this will run even if resource acquisition fails or is canceled.

  27. def onFinalizeCase[G[x] >: F[x]](f: (ExitCase[Throwable]) => G[Unit])(implicit F: Applicative[G]): Resource[G, A]

    Like onFinalize, but the action performed depends on the exit case.

  28. def parZip[G[x] >: F[x], B](that: Resource[G, B])(implicit arg0: Sync[G], arg1: Parallel[G]): Resource[G, (A, B)]

    Allocates two resources concurrently, and combines their results in a tuple.

    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.

    Note that Resource also comes with a cats.Parallel instance that 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)
    
       val release = IO(println(s"Releasing $$name"))
       Resource.make(acquire)(release)
     }
    
    val r = (mkResource("one"), mkResource("two"))
               .parMapN((s1, s2) => s"I have $s1 and $s2")
               .use(msg => IO(println(msg)))
    Definition Classes
    ResourceLike
  29. def parZip_[G[x] >: F[x], B](that: Resource[G, B])(implicit arg0: Sync[G], arg1: Parallel[G]): Resource[G, (A, B)]

    Implementation of parZip, which is declared in ResourceLike

    Implementation of parZip, which is declared in ResourceLike

    Attributes
    protected
  30. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  31. def toString(): String
    Definition Classes
    AnyRef → Any
  32. def use[G[x] >: F[x], B](f: (A) => G[B])(implicit F: BracketThrow[G]): G[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

    Definition Classes
    ResourceLike
  33. def use_[G[x] >: F[x], B](f: (A) => G[B])(implicit F: BracketThrow[G]): G[B]

    Implementation of use, which is declared in ResourceLike

    Implementation of use, which is declared in ResourceLike

    Attributes
    protected
  34. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  35. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  36. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

Inherited from ResourceLike[F, A]

Inherited from AnyRef

Inherited from Any

Ungrouped