1

是否有一个方便的提取器是->运算符的反射(在数学意义上)?

例如,这有效:

scala> val y = 1 -> 2 -> 3
y: ((Int, Int), Int) = ((1,2),3)

scala> y match { case ((a,b),c) => s"Values are : $a, $b, and $c" }
res0: String = Values are : 1, 2, and 3

但这不会:

scala> y match { case a -> b -> c => s"Values are : $a, $b, and $c" }
<console>:9: error: not found: value ->
              y match { case a -> b -> c => s"Values are : $a, $b, and $c" }
                                    ^
<console>:9: error: not found: value a
              y match { case a -> b -> c => s"Values are : $a, $b, and $c" }
                                                            ^

当您有许多嵌套元组时,您可以看到这将如何有用。如果您想知道,我的嵌套元组是由外部库生成的,所以我不能自己“扁平化”。

4

1 回答 1

0

下一个试试

scala> y match { case ((z01,z02),z2) => println(s"$z01 -> $z02 -> $z2") }
1 -> 2 -> 3

scala>
于 2015-11-18T11:14:12.923 回答