片段1:
val str = "HELP" val perfectTuple: (String,String) = str match { case "NO-HELP" => ("First Help","Second Help") case "OTHER-HELP" => ("I won't Help!","Even,I won't Help!") case "HELP" => (null,"Single Help") case _ => throw new NoSuchMethodException }
Snippet2:
val str = "HELP" val (firstPart:String,secondPart:String) = str match { case "NO-HELP" => ("First Help","Second Help") case "OTHER-HELP" => ("I won't Help!",I won't Help!") case "HELP" => (null,"Single Help") case _ => throw new NoSuchMethodException }
==========================
两个片段之间的差异很小.一个将返回的元组存储到tuple2类型的值“perfectTuple”中,并且这个元素成功执行.
另一个从元组2中提取值并将它们存储到字符串值中并抛出运行时’scala.matchError’.
这是scala中的错误吗?
我在scala 2.10.5和2.11.7上尝试过这个
提前致谢.
=============
还有一个场景,我可以从模式匹配中为字符串赋值null,这个非常完美:
Snippet3:
val str = "HELP" val assignNullToString: String = str match { case "NO-HELP" => "ONE" case "OTHER-HELP" => "TWO" case "HELP" => null case _ => throw new NoSuchMethodException }
所以我假设,它不是我为String分配null导致问题,它与Tuple有关吗?和Snippet 2有什么问题,而Snippet 1运行得非常好.
解决方法
现在,我还没有看过Tuple2.unapply的优化代码,但我猜想在某些时候它会对元组的值进行类型匹配.
一个人根本无法在null上键入匹配.
val str: Any = null str match { case _: String => "yay" case other => "damn" } |-> res1: String = damn
更新
让我们稍微讨论你的第二个例子:
val str = "HELP" val (firstPart:String,"Single Help") case _ => throw new NoSuchMethodException }
当我们提取比赛时,我们得到:
val tuple: (String,String) = str match { case "NO-HELP" => ("First Help","Single Help") case _ => throw new NoSuchMethodException }
现在,你把它放到Tuple2的unapply函数中.看看签名:
def unapply[A,B](tuple: Tuple2[_,_]): Option[(A,B)]
因此,传入元组值的类型将被删除!
但是,当你说
val (first: String,second: String) = tuple
您正在使用类型参数[String,String]调用Tuple2.unapply,显式要求结果为(String,String).
为了能够返回Option [(String,String)],unapply函数必须键入两个值匹配.
可以想象Tuple2伴侣对象看起来像,但事实上更有效和复杂:
object Tuple2 { def apply[A,B](_1: A,_2: B): Tuple2[A,B] = new Tuple2(_1,_2) def unapply[A,B)] = { val a: Option[A] = tuple._1 match { case a: A => Some(a) } val b: Option[B] = tuple._2 match { case b: B => Some(b) } a.zip(b).headOption }
现在这里是抛出MatchError的行:
val a: Option[A] = tuple._1 match { case a: A => Some(a) }
正如我之前所说,我没有看过Tuple2的优化代码,但我有理由相信这与实际发生的情况非常接近.
你当然可以放松一下你的要求:
val str = "HELP" val (firstPart,secondPart) = str match { case "NO-HELP" => ("First Help","Single Help") case _ => throw new NoSuchMethodException }
对大多数情况应该足够好,但可能只会推迟疼痛.
编辑2
我建议始终要注意这样一个事实:当使用提取器绑定val时,会使用语法糖进行模式匹配,因为模式匹配可能总是失败并出现匹配错误.
以下是一些明显的例子,我看到过几次不那么明显的事情.
val foo = ("lorem",2) val (lorem: String,bad: String) = foo // fail case class Bar(name: String,age: Option[Int]) val bar = Bar("Sam",None) val Bar(name,Some(age)) = bar // fail