monifu.syntax

TryThenClose

implicit final class TryThenClose[T <: AnyRef { def close(): Unit }] extends AnyVal

Implementation for a tryThenClose extension method meant for closeable resources, like input streams, for safe disposal of resources.

This example:

import monifu.syntax.TryThenClose
import java.io.{FileReader, BufferedReader}

val firstLine =
  new BufferedReader(new FileReader("file.txt")).tryThenClose { in =>
    in.readLine()
  }

Is equivalent to this:

import java.io.{FileReader, BufferedReader}

val fistLine = {
  val in = new BufferedReader(new FileReader("file.txt"))
  try {
    in.readLine()
  } finally {
    in.close()
  }
}

Actually for this to work it doesn't have to be a Java Closable as the type-checking is based on structural typing (yet because it's implemented as a macro, the actual call is not reflective).

import monifu.syntax.TryThenClose
class Foo { def close() = println("CLOSING") }

new Foo().tryThenClose { _ =>
  println("Yay!")
}
Linear Supertypes
AnyVal, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. TryThenClose
  2. AnyVal
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new TryThenClose(self: T)

Value Members

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

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

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

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

    Definition Classes
    Any
  5. def getClass(): Class[_ <: AnyVal]

    Definition Classes
    AnyVal → Any
  6. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  7. val self: T

  8. def toString(): String

    Definition Classes
    Any
  9. macro def tryThenClose[R](f: (T) ⇒ R): R

Inherited from AnyVal

Inherited from Any

Ungrouped