Scala 大小写匹配默认值 [英] Scala case match default value

查看:39
本文介绍了Scala 大小写匹配默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取匹配大小写的默认值?

How can I get the default value in match case?

//Just an example, this value is usually not known
val something: String = "value"

something match {
    case "val" => "default"
    case _ => smth(_) //need to reference the value here - doesn't work
}

更新:我发现我的问题没有被真正理解,这就是为什么我要展示一个更接近我正在研究的真实事物的例子:

UPDATE: I see that my issue was not really understood, which is why I'm showing an example which is closer to the real thing I'm working on:

val db =    current.configuration.getList("instance").get.unwrapped()
            .map(f => f.asInstanceOf[java.util.HashMap[String, String]].toMap)
            .find(el => el("url").contains(referer))
            .getOrElse(Map("config" -> ""))
            .get("config").get match {
                case "" => current.configuration.getString("database").getOrElse("defaultDatabase")
                case _  => doSomethingWithDefault(_)
            }

推荐答案

something match {
    case "val" => "default"
    case default => smth(default)
}

它不是关键字,只是别名,所以这也可以:

It is not a keyword, just an alias, so this will work as well:

something match {
    case "val" => "default"
    case everythingElse => smth(everythingElse)
}

这篇关于Scala 大小写匹配默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆