我已经定义了“使用”功能如下:
def using[A, B <: {def close(): Unit}] (closeable: B) (f: B => A): A =
try { f(closeable) } finally { closeable.close() }
我可以这样使用它:
using(new PrintWriter("sample.txt")){ out =>
out.println("hellow world!")
}
现在我很好奇如何定义“使用”函数来获取任意数量的参数,并能够分别访问它们:
using(new BufferedReader(new FileReader("in.txt")), new PrintWriter("out.txt")){ (in, out) =>
out.println(in.readLIne)
}