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: 449

Filed under:
|
|
|

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

Related posts about scala

Related posts about using