Scala's 22 limitation

I’ve noticed many are unpleased with this limit. There’s also some articles talking about it: https://underscore.io/blog/posts/2016/10/11/twenty-two.html

Is there a chance of discarding this limit?
I saw that the cause for this limit is the way Tuple and Function handles generics.

My first thought was maybe it could be bypassed by changing te way a Tuple works by having it built as a sort of collection which accepts AnyVal, and having Function’s arguments as a Tuple, but it may cause problems when you want to do the following, for example: declaring a List[Tuple[Int, String]], that is a List of Tuple which any of the Tuple objects is a 2-tuple which the first is an Int object and the second is a String object.

Another way to solve it I had in mind is to allow accepting a list of types in generic classes, instead of declaring Cls[T, U, V], add a support for a new sort of declaration Cls[3] (accept int instead of identifier), and refering to the types inside the class using [0], [1] and [2]. When needed an unbounded list of types define it as Cls[], and inside the class use [] to iterate over the types, and maybe add [_] or any other symbol like [length] to check the size of that list.

One advantage, is eliminating the need to name the types, which usually ends up with random letters, starting with T.

I suppose the only way to implement such a thing will be using reflection (holding list of class objects), which is not very efficient.

Yes. It’s gone in dotty. https://dotty.epfl.ch/docs/reference/dropped/limit22.html

2 Likes

Thanks for the quick answer. I am kinda new to this language. I only read about it, and didn’t really tried to write code yet. I saw this limit from looking at the documentaion of the latest version. There was there Tuple0…Tuple22, Product0…Product22 and Function0…Function22. I started reading about it only after seeing this. Are those deprecated, or the elimination of the limit is planned but not yet implemented?

1 Like

Dotty is, essentially, Scala 3.0. So the limit is gone in an upcoming version of the language, which is currently in development.

2 Likes

@jducoeur is correct. If you want to work around this limitation in Scala 2, you can use hlists.

Compile time for HLists grows exponentially with number of elements and becomes too long way before you reach 22.

IMHO this is an incorrect assumption.

Probably depends on the use case but in my experience it’s often not an issue in practice.