Why does the Scala library only defines tuples up

2020-02-13 07:19发布

问题:

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?

回答1:

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.



回答2:

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).



回答3:

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



回答4:

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



标签: scala