如何在Scala中匹配多个值?

比方说,我想要使用相同的代码处理来自远程服务的多个返回值。 我不知道如何在Scala中expression这一点:

code match { case "1" => // Whatever case "2" => // Same whatever case "3" => // Ah, something different } 

我知道我可以使用Extract Method并调用它,但是在调用中仍然存在重复。 如果我使用Ruby,我会这样写:

 case code when "1", "2" # Whatever when "3" # Ah, something different end 

请注意,我简化了这个例子,因此我不想在正则expression式或其他types上进行模式匹配。 匹配值实际上是复杂的值。

你可以做:

 code match { case "1" | "2" => // whatever case "3" => } 

请注意,您不能将模式的某些部分绑定到名称 – 目前无法执行此操作:

 code match { case Left(x) | Right(x) => case null => }