嵌套 Scala 匹配器为什么 Some(Some(1),1) 无法匹配?
嵌套匹配似乎不起作用,这是一个奇怪的限制。
该行为的一个示例如下:
Some(Some(1),2) match {
| case Some(Some(a),b) => a
| case e => e
| }
<console>:9: error: wrong number of arguments for <none>: (x: (Some[Int], Int))Some[(Some[Int], Int)]
case Some(Some(a),b) => a
^
<console>:9: error: not found: value a
case Some(Some(a),b) => a
^
这有效:
Some(Some(1),2) match {
case Some(a) => a match {
case (Some(a),b) => "yay"
case e => "nay"
}
}
现在,我只是一个傻瓜还是有更好的方法来实现这一目标?
It seems that nested matching doesn't work, which is a strange limitation.
An example of the behaviour follows:
Some(Some(1),2) match {
| case Some(Some(a),b) => a
| case e => e
| }
<console>:9: error: wrong number of arguments for <none>: (x: (Some[Int], Int))Some[(Some[Int], Int)]
case Some(Some(a),b) => a
^
<console>:9: error: not found: value a
case Some(Some(a),b) => a
^
This works:
Some(Some(1),2) match {
case Some(a) => a match {
case (Some(a),b) => "yay"
case e => "nay"
}
}
Now, am I just being a twit or is there a better way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
什么是一些(一些(1),2)? (Option (of Int) and Int) 元组的选项?这是有效的:
请注意元组周围的附加括号 - 元组太少是一个常见的错误。
What is Some (Some(1),2)? An Option of Tuple of (Option (of Int) and Int)? This works:
Note the additional parenthesis around the tuple - it's a common mistake to have too few of them.