Search code examples
scalascala-catscats-effect

How to chain more than 23 elements in a tuple in Scala Cats?


In Scala I am making use of the Cats Library and I am using the mapN() function. Lets say I have created the following case class with 22 elements. The following code will work without any problem.

case class Example1 (element1: String , element2: String ..... element22)
('string1', ...'string22').mapN(Example1.apply)

Lets say I want a element23. But unfortunately the compiler will complain that there are arguments missing because the mapping is not done correctly. Is there any way of chaining the 23rd element because I saw that there is a tuple of max 22 elements handled by the mapN() function.


Solution

  • To my knowledge, there is no support for mapN for tuples of arity > 22. One could probably change that in cats by fiddling around with maxArity here (though that would require doing that for Scala 3 only).

    However: for your specific use case (converting tuples to a case class), Scala 3 (which I assume you are using because Scala 2 does not support tuples of arity > 22) would offer a simple solution: see Converting between tuples and case classes in Scala 3 and this runnable scastie example.