Scala case match default value

前端 未结 3 1220
你的背包
你的背包 2021-01-31 08:30

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 \         


        
3条回答
  •  隐瞒了意图╮
    2021-01-31 08:41

    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)
    }
    

提交回复
热议问题