Scala本地返回? [英] Scala local return?
本文介绍了Scala本地返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚发现 return
在下面的闭包将从函数 findPackage
I've just discovered that return
s in following closure will return from function findPackage
def findPackage(name: String, suffix: Option[String] = None): Path = {
logger.debug("Looking for package {} with suffix {}", name, suffix)
val path: Path = using(Files.newDirectoryStream(appDir)) {dirs =>
for (val path <- dirs) {
val matcher = packagePattern.matcher(path.getFileName.toString)
if (matcher.matches() && matcher.group(1).equals(name))
if (suffix.isDefined) {
if (matcher.group(2) != null && matcher.group(2).equals(suffix.get))
return path
} else
return path
}
throw new PackageNotFoundException(this, name, suffix)
}
logger.debug("Found package is {}", path)
path
}
我能以某种方式做一个本地回来吗?谢谢。
Can I somehow do a local return please? Thank you.
推荐答案
或者你可以摆脱你的循环,替换为你想做的:find
Or you could get rid of your loop and replace it with what you're trying to do: "find"
def findPackage(name: String, suffix: Option[String] = None): Path = {
logger.debug("Looking for package {} with suffix {}", name, suffix)
def matching(path : Path) : Boolean = {
val matcher = packagePattern.matcher(path.getFileName.toString)
matcher.matches && matcher.group(1).equals(name) && (!suffix.isDefined || (matcher.group(2) != null && matcher.group(2).equals(suffix.get))
}
val path: Path = using(Files.newDirectoryStream(appDir)) {dirs =>
dirs find matching getOrElse {throw new PackageNotFoundException(this, name, suffix)}
}
logger.debug("Found package is {}", path)
path
}
这篇关于Scala本地返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文