模式匹配通过斯卡拉

Tim*_*mmy 1 scala

我想做这样的事情:

val a = v match {
   case 1 => 1
   case 2 if (condition) => logging
   case 2 if (other conditions) => 3
   case 2 if (more conditions) => 4
   case _ => 5
}
Run Code Online (Sandbox Code Playgroud)

我希望这只返回第一个案例2的日志,但是否则会通过其他方式返回以查看返回的内容

编辑:更新

Dyl*_*lan 5

Scala的案例匹配并没有"落伍",但如果我理解正确,这将做你想要的.

val a = v match {
  case 1 => 1
  case 2 => {
    logging // I'm assuming `logging` is some Unit
    if(some condition) 3
    else if(some other condition) 4
    else 5
  }
  case _ => 5
}
Run Code Online (Sandbox Code Playgroud)