I'm learning scala and like the ability to come up with custom control structure, and as there's no using
structure to close resources, I thought it'd be handy to write one. Then I found this snippet of code from the book, Beginning Scala, by David Pollak.
using (Source.fromFile("file")) { source =>
println(source.mkString)
}
def using[A <: { def close() }, B](closable: A)(body: A => B): B =
try
body(closable)
finally
closable.close()
But I wonder whether would be possible to have something like:
using (val source = Source.fromFile("file")) println(source.mkString)