Class/Object

scalaz.managed

Managed

Related Docs: object Managed | package managed

Permalink

sealed abstract class Managed[A] extends AnyRef

An example Scala program to copy data from one handle to another might look like this:

def main(args: Array[String]) =
  withFileInputStream("inFile.txt")(is =>
    withFileOutputStream("outFile.txt")(os =>
      copy(is, os)
    )
  )

// A hypothetical function for safely using a FileInputStream
def withFileInputStream[A](path: String)(f: FileInputStream => IO[A]): IO[A]

// A hypothetical function for safely using a FileOutputStream
def withFileOutputStream[A](path: String)(f: FileOutputStream => IO[A]): IO[A]

// A hypothetical function that copies data from an InputStream to an OutputStream
def copy(is: InputStream, os: OutputStream): IO[Unit]

withFileInputStream and withFileOutputStream are a few of many functions that acquire some resource in an exception-safe way. These functions take a callback function as an argument and they invoke the callback on the resource when it becomes available, guaranteeing that the resource is properly disposed if the callback throws an exception. These functions usually have a type that ends with the following pattern:

                      Callback
                    -------------
def withXXX[A](...)(f: R => IO[A]): IO[A]

Here are some examples other examples of this pattern:

def withSocket[A](addr: SocketAddress)(f: Socket => IO[A]): IO[A]
def withDB[A](config: DBConfig)(f: DB => IO[A]): IO[A]

Acquiring multiple resources in this way requires nesting callbacks. However, you can wrap anything of the form A => IO[R] => IO[R] in the Managed monad, which translates binds to callbacks for you:

import scalaz.managed._

def fileInputStream(path: String): Managed[FileInputStream] =
  Managed(new Forall[Lambda[A => (FileInputStream => IO[A]) => IO[A]]] {
    def apply[A] = withFileInputStream(path)
  }

def fileOutputStream(path: String): Managed[FileOutputStream] =
  Managed(new Forall[Lambda[A => (FileOutputStream => IO[A]) => IO[A]]] {
    def apply[A] = withFileOutputStream(path)
  }

 def main(args: Array[String]) =
   Managed.run(
     for {
       in  <- fileInputStream("inFile.txt")
       out <- fileOutputStream("outFile.txt")
       _   <- copy(in, out).liftIO[Managed]
     } yield ()
   )
Self Type
Managed[A]
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Managed
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def apply[R](pure_: (A) ⇒ IO[R]): IO[R]

    Permalink
    Attributes
    protected

Concrete 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. final def flatMap[B](f: (A) ⇒ Managed[B]): Managed[B]

    Permalink
  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 map[B](f: (A) ⇒ B): Managed[B]

    Permalink
  14. final def ne(arg0: AnyRef): Boolean

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

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

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

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

    Permalink
    Definition Classes
    AnyRef → Any
  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