在Python中,你可以使用
它大致的语法如下:
with
来避免使用try {} catch {} finally {}
语句块(参见Python中的关键字“with”有什么用?)。我记得在Scala中看到过类似的替代方案,但现在找不到了。它大致的语法如下:
def using[O](r: {def close()})(doit: () => O): O = try {
doit()
} finally {
r.close
}
using(myWriter){() => myWriter.println("something or another")}
它是内置在2.10中的吗,还是我需要一个单独的库来使用?