An object that may hold resources until it is closed.
An object that may hold resources until it is closed.
Similar to java.lang.AutoCloseable except the close operation is monadic.
A container of a value and a function to release the value
.
the monadic type of release
the type of value
Releases value and all resource dependencies during creating value.
After release, value should not be used if:
This Resource will become a case class. Use Resource.apply instead of new Serializable with Resource[F, A] { ... }
.
The data structure that provides automatic resource management.
The data structure that provides automatic resource management.
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()
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
(Since version 3.0.0) Use Resource instead.
The companion object of ResourceT that contains converters and type classes.
The companion object of ResourceT that contains converters and type classes.
There are some implicit method that provides cats.Monads as monad transformers of F
.
Those monads running will collect all resources,
which will be open and release altogether when ResourceTOps.run is called.
The type-level Pimpl in order to prevent the Scala compiler seeing the actual type of ResourceT
The namespace that contains the covariant ResourceT.
Usage:
import com.thoughtworks.zerocost.resourcet._