相关疑难解决方法(0)

Scala: Pattern matching when one of two items meets some condition

I'm often writing code that compares two objects and produces a value based on whether they are the same, or different, based on how they are different.

So I might write:

val result = (v1,v2) match {
  case (Some(value1), Some(value2)) => "a"
  case (Some(value), None)) => "b"
  case (None, Some(value)) => "b"
  case _ = > "c"
}
Run Code Online (Sandbox Code Playgroud)

Those 2nd and 3rd cases are the same really, so I tried writing:

val result = (v1,v2) match {
  case (Some(value1), Some(value2)) …
Run Code Online (Sandbox Code Playgroud)

scala pattern-matching extractor

10
推荐指数
3
解决办法
1万
查看次数

标签 统计

extractor ×1

pattern-matching ×1

scala ×1