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

后端 未结 4 1130
予麋鹿
予麋鹿 2020-12-20 11: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 s

相关标签:
4条回答
  • 2020-12-20 12:13

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

    0 讨论(0)
  • 2020-12-20 12:16

    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

    0 讨论(0)
  • 2020-12-20 12:17

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

    0 讨论(0)
  • 2020-12-20 12:24

    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.

    0 讨论(0)
提交回复
热议问题