Joa*_*oan 3 scala tuples partialfunction
如果我做:
val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol"))
l map { t => t._1 + t._2 }
Run Code Online (Sandbox Code Playgroud)
没关系.
如果我做:
val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol"))
l map { case (b, n) => b + n }
Run Code Online (Sandbox Code Playgroud)
也没关系.
但如果我这样做:
val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol"))
l map { (b, n) => b + n }
Run Code Online (Sandbox Code Playgroud)
不起作用.
为什么我应该使用"case"关键字来使用命名元组?
2.11的错误消息更具说明性:
scala> l map { (b, n) => b + n }
<console>:9: error: missing parameter type
Note: The expected type requires a one-argument function accepting a 2-Tuple.
Consider a pattern matching anonymous function, `{ case (b, n) => ... }`
l map { (b, n) => b + n }
^
<console>:9: error: missing parameter type
l map { (b, n) => b + n }
^
Run Code Online (Sandbox Code Playgroud)
对于申请,您将获得"自动翻译":
scala> def f(p: (Int, Int)) = p._1 + p._2
f: (p: (Int, Int))Int
scala> f(1,2)
res0: Int = 3
Run Code Online (Sandbox Code Playgroud)
你提供了两个args而不是一个args.
但是你没有得到自动解决.
人们一直希望它以这种方式工作.
| 归档时间: |
|
| 查看次数: |
3753 次 |
| 最近记录: |