scala - 尽量避免在我的方法中有两个 try 块
问题描述
如何避免if
在我的代码中重复条件:
def download() =
Action {
val out = new FileWriter("/tmp/foo.txt")
Try {
val chars = new Array[Char](1024)
util.Arrays.fill(chars, '.')
chars(1024) = '\n'
out.write(chars)
} match {
case Failure(_) =>
if (out != null) out.close()
println("failed")
case Success(_) =>
if (out != null) out.close()
println("success")
}
}
我试图从以下方面进行调整:
def download() =
Action {
Try {
val out = new FileWriter("/tmp/foo.txt")
try{
val chars = new Array[Char](1024)
util.Arrays.fill(chars, '.')
chars(1024) = '\n'
out.write(chars)
} if (out != null) out.close()
} match {
case Failure(_) =>
println("failed")
case Success(_) =>
println("success")
}
}
但是这样我try
在 Scala中就有了一个 Java Try
,这看起来不正确。如何避免有两个 try 块?我基本上只是希望在方法完成执行后清理我获得的资源。
解决方案
如果您不能使用Using
,您可以使用try-catch-finally
:
def download() =
Action {
val out = new FileWriter("/tmp/foo.txt")
try {
val chars = new Array[Char](1024)
util.Arrays.fill(chars, '.')
chars(1024) = '\n'
out.write(chars)
println("success")
} catch {
case _: Throwable =>
println("failed")
} finally {
out.close()
}
}
或贷款模式:
def withFileWriter[A](str: String)(f: FileWriter => A): Try[A] = Try {
val out = new FileWriter("/tmp/foo.txt")
try {
f(out)
} finally {
out.close()
}
}
withFileWriter("/tmp/foo.txt"){ out =>
util.Arrays.fill(chars, '.')
chars(1024) = '\n'
out.write(chars)
} match {
case Failure(_) =>
println("failed")
case Success(_) =>
println("success")
}
如果你有 Cats Effect(并且如果你用合法的东西替换 Try),你可以使用 Resource
val resource = Resource.make {
IO(new FileWriter("/tmp/foo.txt"))
} { out =>
IO(out.close())
}
resource.use(out => IO {
util.Arrays.fill(chars, '.')
chars(1024) = '\n'
out.write(chars)
}).attempt.map {
case Failure(_) =>
println("failed")
case Success(_) =>
println("success")
}.unsafeRunSync
推荐阅读
- reactjs - 如何传入当前日期 React JS Mongo
- regex - BASH - 使用正则表达式从 netstat 解析 PID
- python - 使用嵌套元组初始化集合
- python - Pandas DataFrame 表索引
- sql - Oracle 中的嵌套选择结果集
- python - Discord.py 获取公会成员的所有名称和歧视(python)
- python - 调用 __str__ 方法来产生输出?
- javascript - 无法在“WebGL2RenderingContext”上执行“texImage2D”。从源“exampale.com”访问“imageurl.com”上的图像已被 CORS 阻止
- r - Caret Train 函数是否在 R 中默认将因子转换为虚拟变量?
- python - Python:读取txt时如何处理右单引号