Skip to content

Instantly share code, notes, and snippets.

@takscape
Created October 6, 2011 13:15
Show Gist options
  • Save takscape/1267370 to your computer and use it in GitHub Desktop.
Save takscape/1267370 to your computer and use it in GitHub Desktop.
Scala: ローンパターン2
import collection.JavaConversions._
import java.net.URL
import org.apache.commons.io.IOUtils
object scalatest
{
def closing[T <: {def close(): Unit}, R](a: T)(f: T => R): R = {
try {
f(a)
} finally {
a.close()
}
}
def main(args: Array[String])
{
closing(new URL("http://www.google.com/").openStream) { s =>
println(IOUtils.toString(s, "UTF-8"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment