Scala:“使用”功能
发布时间:2020-12-16 09:42:02 所属栏目:安全 来源:网络整理
导读:我定义了’using’功能如下: 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’功能如下:
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) } 解决方法
有人已经这样做了 – 它叫做
Scala ARM。
从自述: import resource._ for(input <- managed(new FileInputStream("test.txt")) { // Code that uses the input as a FileInputStream } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |