Bry*_*unt 6 scala case pattern-matching extractor scala-option
似乎嵌套匹配不起作用,这是一个奇怪的限制.
行为的一个例子如下:
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
^
Run Code Online (Sandbox Code Playgroud)
这有效:
Some(Some(1),2) match {
case Some(a) => a match {
case (Some(a),b) => "yay"
case e => "nay"
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我只是一个蠢货还是有更好的方法来实现这一目标?
use*_*own 12
什么是一些(一些(1),2)?(元素选项(Int)和Int)的元组选项?这有效:
scala> Some ((Some (1), 2)) match {
| case Some ((Some (a), b)) => a
| case e => e }
res13: Any = 1
Run Code Online (Sandbox Code Playgroud)
注意元组周围的附加括号 - 它们中包含太少的常见错误.
| 归档时间: |
|
| 查看次数: |
2001 次 |
| 最近记录: |