我刚刚发现 return
以下闭包中的 s 将从函数 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
}
我可以以某种方式在本地退货吗?谢谢你。
最佳答案
或者您可以摆脱循环并将其替换为您正在尝试执行的操作:“查找”
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本地返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9707210/