我正在对冒号分隔的String进行一些模式匹配,如下所示:
case s:String => s.split(":") match {
case Array("foo","bar") => ...
case Array("hello",_,_) => ...
...
}
如何重新排列代码以为s.split(":")
返回的数组指定名称?
我尝试过以下无效:
case s:String => val x = s.split(":") match {
case Array("foo","bar") => // try to use x here
...
}
答案 0 :(得分:9)
case s:String => s.split(":") match {
case x @ Array("foo","bar") => ...
case x @ Array("hello",_,_) => ...
...
}
或者
case s:String =>
val x = s.split(":")
x match {
case Array("foo","bar") => ...
case Array("hello",_,_) => ...
case Array(aStr, "bar") => println(aStr.toUpperCase)
...
}