Class/Object

cats.effect

Resource

Related Docs: object Resource | package effect

Permalink

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 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 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
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Resource
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. def flatMap[B](f: (A) ⇒ Resource[F, B]): Resource[F, B]

    Permalink

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

  10. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  11. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  12. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  17. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  18. def use[B](f: (A) ⇒ F[B])(implicit F: Bracket[F, Throwable]): F[B]

    Permalink

    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

  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped