在 Scala 中如何访问每个案例块中的匹配案例值
In Scala how to access match case value in each case block
我想访问 case 块中的 case 值
val a = "hello"
a match {
case "hello" | "Hi" =>
println("hello") // how can I access the right case value? Hello or Hi?
case _ =>
print("NA")
}
您可以像这样引用匹配值:
a match {
case str @ ("hello" | "Hi") => println(str)
case _ => print("NA")
}
另一种方式可能是 -
a match {
case str if str == "hello" | str == "Hi" => println(str)
case _ => println("NA")
}
我想访问 case 块中的 case 值
val a = "hello"
a match {
case "hello" | "Hi" =>
println("hello") // how can I access the right case value? Hello or Hi?
case _ =>
print("NA")
}
您可以像这样引用匹配值:
a match {
case str @ ("hello" | "Hi") => println(str)
case _ => print("NA")
}
另一种方式可能是 -
a match {
case str if str == "hello" | str == "Hi" => println(str)
case _ => println("NA")
}