开发者

Scala: function/method application and tuples

开发者 https://www.devze.com 2023-02-13 05:18 出处:网络
I stumbled across a pretty interesting behavior in Scala. scala> def foo(t: (Int, Int, Int)): Int = t._1

I stumbled across a pretty interesting behavior in Scala.

scala> def foo(t: (Int, Int, Int)): Int = t._1 
foo: (t: (Int, Int, I开发者_如何学Gont))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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消