Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are scala functions limited to 22 parameters?

Tags:

scala

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?

like image 290
Jackson Davis Avatar asked Nov 11 '10 07:11

Jackson Davis


People also ask

What does => mean in scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

What is a parameter in scala?

Implicit parameters are the parameters that are passed to a function with implicit keyword in Scala, which means the values will be taken from the context in which they are called.

Can I pass parameter to class in scala?

Singleton Objects in Scala We can call it's methods directly, we can't pass parameters to its primary constructor.

Is scala pass by value?

By default scala use call-by-value for passing arguments to a function.


3 Answers

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.

like image 92
Theo Avatar answered Oct 23 '22 19:10

Theo


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.

like image 40
Apocalisp Avatar answered Oct 23 '22 19:10

Apocalisp


It's mostly arbitrary, but there are some underlying limits on the JVM that dictate roughly what the limit needs to be.

The main issue is pattern-matching on case classes. If a case class allowed to be much bigger then the generated pattern-match code could very easily exceed the maximum valid method size. Everything else (Product, Function, Tuple, ...) just follows the 22-parameter limit that was therefore chosen for case classes.

Also... If you're writing functions/tuples with > 22 parameters then you're probably overdue for a redesign :)

like image 12
Kevin Wright Avatar answered Oct 23 '22 19:10

Kevin Wright