Type/Object

com.thoughtworks.zerocost.resourcet

ResourceT

Related Docs: object ResourceT | package resourcet

Permalink

type ResourceT[F[+_], +A] = resourcet.OpacityTypes.ResourceT[F, A]

The data structure that provides automatic resource management.

Source
resourcet.scala
Example:
  1. ResourceT can be used as a monad transformer for scala.Function0

    import scala.Function0
    import cats.instances.function._
    import com.thoughtworks.zerocost.resourcet._
    type RAII[A] = ResourceT[Function0, A]

    Given a resource that creates temporary files

    trait MyResource extends AutoCloseable {
      def inUse(): Unit
    }
    val myResourceStub0 = stub[MyResource]
    val myResourceStub1 = stub[MyResource]
    val myResourceFactoryMock = mockFunction[MyResource]
    myResourceFactoryMock.expects().returns(myResourceStub0)
    myResourceFactoryMock.expects().returns(myResourceStub1)
    val resource: RAII[MyResource] = ResourceT.autoCloseable[Function0, MyResource](myResourceFactoryMock)

    when using temporary file created by resouce in a for / yield block, those temporary files should be available.

    import cats.syntax.all._
    val usingResouce = for {
      tmpFile0 <- resource
      tmpFile1 <- resource
    } yield {
      tmpFile0 should be(myResourceStub0)
      tmpFile1 should be(myResourceStub1)
      tmpFile1.inUse()
    }

    and those files should have been deleted after the for / yield block.

    usingResouce.run.apply()
    ((myResourceStub0.inUse _): () => Unit).verify().never()
    ((myResourceStub0.close _): () => Unit).verify().once()
    ((myResourceStub1.inUse _): () => Unit).verify().once()
    ((myResourceStub1.close _): () => Unit).verify().once()
Note

This ResourceT type is an opacity alias to F[Resource[F, A]]. All type classes and helper functions for this ResourceT type are defined in the companion object ResourceT

Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def getClass(): Class[_]

    Permalink
    Definition Classes
    Any

Concrete Value Members

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

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

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

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

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

    Permalink
    Definition Classes
    Any
  6. def hashCode(): Int

    Permalink
    Definition Classes
    Any
  7. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  8. def toString(): String

    Permalink
    Definition Classes
    Any

Ungrouped