Why are scala functions limited to 22 parameters?

Not that I've actually come close to that limit, but Ive always wondered: Why do they stop at Function22/Tuple22. JVM restriction? Arbitrary choice?


Functions and tuples are rewritten as objects by the compiler, and only Function0 through Function22 and Tuple0 through Tuple22 are defined. I think the limit of 22 is entirely arbitrary, but the reason for having a limit is not.

Think of it this way: to run a Scala application the classes needed to run it must be present. If the compiler would dynamically create classes for functions then those classes would not be included in the Scala library JAR, so you would have to include them in your application. That could work, but then you would have the problem of what the classes' fully qualified names should be: if they were the same for all apps then you would have clashes since libraries would contain the same classes, and if the names were not the same you would end up with incompatibilities -- functions from libraries wouldn't be the same as functions in your app.


There is no such limit. Even if the standard libraries only define up to Function22, you can define Function23 if you need it, up to the JVM limit. Or you can group arguments into tuples. Or you could just stop pretending that any function takes more than one argument:

a => b => c => d => e => ...

Curried functions can take as many arguments as you want, up to the limit of your stack size.