Scala本地返回?

标签 scala controls closures

我刚刚发现 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/

相关文章:

Scala,如何指定具有多个特征的函数参数?

android - Android 上的 Qt 控件如此之小

.net - 所有者绘制 TextBox 以在 WinForms 中使用

arrays - 具有多个闭包的通用函数不起作用

scala - 简单的 ScalaFx TableView 示例未编译

scala - 使用返回 Future 的二元运算折叠序列

scala - 保护 Play 框架和 OAuth2 上的 REST API 的安全

c# - 如何将标准控件属性固定为所需值?

Swift 命令行工具在 Debug模式下构建,但不在 Release模式下构建

javascript - 实际上什么时候创建闭包?