Scala: "using" keyword
Posted
by Albert Cenkier
on Stack Overflow
See other posts from Stack Overflow
or by Albert Cenkier
Published on 2010-03-07T10:56:21Z
Indexed on
2010/03/08
8:36 UTC
Read the original article
Hit count: 470
i've defined 'using' keyword as following:
def using[A, B <: {def close(): Unit}] (closeable: B) (f: B => A): A =
try { f(closeable) } finally { closeable.close() }
i can use it like that:
using(new PrintWriter("sample.txt")){ out =>
out.println("hellow world!")
}
now i'm curious how to define 'using' keyword to take any number of parameters, and be able to access them separately:
using(new BufferedReader(new FileReader("in.txt")), new PrintWriter("out.txt")){ (in, out) =>
out.println(in.readLIne)
}
© Stack Overflow or respective owner