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.
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 scalaz.Name
import scalaz.Name import com.thoughtworks.raii.invariant._ type RAII[A] = ResourceT[Name, A]
Given a resource that creates temporary files
import java.io.File val resource: RAII[File] = ResourceT(Name(new Resource[Name, File] { override val value: File = File.createTempFile("test", ".tmp"); override val release: Name[Unit] = Name { val isDeleted = value.delete() } }))
when using temporary file created by resouce
in a for
/ yield
block,
those temporary files should be available.
import scalaz.syntax.all._ val usingResouce = for { tmpFile1 <- resource tmpFile2 <- resource } yield { tmpFile1 shouldNot be(tmpFile2) tmpFile1 should exist tmpFile2 should exist (tmpFile1, tmpFile2) }
and those files should have been deleted after the for
/ yield
block.
val (tmpFile1, tmpFile2) = usingResouce.run.value
tmpFile1 shouldNot exist
tmpFile2 shouldNot exist
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 scalaz.Monads as monad transformers of F
.
Those monads running will collect all resources,
which will be open and release altogether when ResourceT.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 invariant ResourceT.
Usage:
import com.thoughtworks.raii.invariant._