Skip to content

Instantly share code, notes, and snippets.

@colinbes
Created June 17, 2015 20:46
Show Gist options
  • Save colinbes/a7038e037ecc71315b53 to your computer and use it in GitHub Desktop.
Save colinbes/a7038e037ecc71315b53 to your computer and use it in GitHub Desktop.
Scala loan pattern
//Loaner pattern to ensure closing of bufferedsource
...
def using[T](r: BufferedSource)(handler: BufferedSource => T): T = {
try {
handler(r)
} finally {
r.close()
}
}
lazy val versioning:... = {
Option(getClass.getResourceAsStream("/archives.json")) match {
case Some(is) =>
val resource = Source.fromInputStream(is)
using(resource) { input =>
...
}
case _ => ...
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment