Да, вы можете, определив локальный метод:
def findPackage(name: String, suffix: Option[String] = None): Path = {
logger.debug("Looking for package {} with suffix {}", name, suffix)
def search(dirs: List[File]) = { // not sure what the type of dirs actually is
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)
}
val path: Path = using(Files.newDirectoryStream(appDir))(search _)
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)
val path: Path = using(Files.newDirectoryStream(appDir)) {dirs =>
try {
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))
throw new ReturnException(path)
} else
throw new ReturnException(path)
}
throw new PackageNotFoundException(this, name, suffix)
}
catch { case ReturnException(path) => path }
}
logger.debug("Found package is {}", path)
path
}