开发者

Why does the Scala library only defines tuples up to Tuple22?

I'm curious if anyone knows why the Scala library stops at 22 with its tuple type Tuple22?

Does the mysterious number 22 have a special hidden meaning?

Is this an internal开发者_运维知识库 joke of some kind?


This question is not new, see http://scala-programming-language.1934581.n4.nabble.com/Why-tuples-only-to-22-td1945314.html or why FunctionN(0-22) ProductN(1-22) TupleN(1-22)?

AFAIK there is no "technical" explanation for it, they simply had to stop somewhere.


Limit 22 is one of the dropped features of dotty (Scala 3) which now allows tuples of arbitrary arity:

The limits of 22 for... the maximal number of fields in tuple types have been dropped... tuples beyond Tuple22 are erased to a new trait scala.TupleXXL.

For example,

object Main {
  def main(args: Array[String]): Unit = {
    val tuple: Tuple = ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25")
    println(tuple.getClass)
    println(tuple.size)
  }
}

outputs

class scala.TupleXXL
25

For further examples see Add tests for the current tuple API #7633


I believe it has to do with difficulties in implementing a static type system while having variadic (arbitrary-argument) functions. I believe apply can be written in Scala (though not in Haskell, at least not elegantly).


The case class limit has been lifted in 2.11 https://github.com/scala/scala/pull/2305

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜