Scala:函数/方法应用程序和元组
我在 Scala 中偶然发现了一个非常有趣的行为。
scala> def foo(t: (Int, Int, Int)): Int = t._1
foo: (t: (Int, Int, Int))Int
scala> foo(1,2,3)
res23: Int = 1
scala> foo((1,2,3))
res24: Int = 1
反之亦然:
scala> Some(1,2,3,4,5)
res31: Some[(Int, Int, Int, Int, Int)] = Some((1,2,3,4,5))
虽然这种糖非常有用,但我没有找到任何与此相关的文档。所以我的问题基本上是:Scala 语言规范中记录了这一点,以及这是否还有其他影响(如果有)。
问候, 雷鸟
I stumbled across a pretty interesting behavior in Scala.
scala> def foo(t: (Int, Int, Int)): Int = t._1
foo: (t: (Int, Int, Int))Int
scala> foo(1,2,3)
res23: Int = 1
scala> foo((1,2,3))
res24: Int = 1
This also works the other way round:
scala> Some(1,2,3,4,5)
res31: Some[(Int, Int, Int, Int, Int)] = Some((1,2,3,4,5))
While this sugar is extremely useful I did not find any documentation concerning this. So my question is basically: Where is this documented in the Scala Language Specification, and what other implications does this have if any.
Regards,
raichoo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是所谓的自动图元组。我针对语言规范提出了一个错误,该规范对此事保持沉默。
这是 编译源代码。
It is known a Automatic Tupling. I lodged a bug against the language specification, which is silent on this matter.
Here's the relevant part of the compiler source code.