Skip to content

Instantly share code, notes, and snippets.

@alexy
Created March 30, 2012 19:16
Show Gist options
  • Save alexy/2254199 to your computer and use it in GitHub Desktop.
Save alexy/2254199 to your computer and use it in GitHub Desktop.
how do we move val w declaration inside try?
def printFileWriter(name: String): PrintWriter = {
new PrintWriter(new FileWriter(name))
}
def writing[B](fileName: String)(f: PrintWriter => B): B = {
lazy val w = printFileWriter(fileName) // so throws only in try, if ever
try { f(w) }
finally { w.close() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment