The following code is a typical demo of one of shapeless' use case:
def getHList[P <: Product, F, L <: HList](p: P)(implicit gen: Generic.Aux[P, L]): L = {
gen.to(p)
}
val v = getHList(1, 2, 3, 4, 5, 6, 7, 8, 9)
This gives the proper result, unfortunately, it relies on scala's tuple syntactic suger, and doesn't work when the number of argument > 22:
val v = getHList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
(this generates an error that looks this the follow)
[Error] /xxx/HListSuite.scala:41: 29 more arguments than can be applied to method getHList: (p: P)(implicit gen: shapeless.Generic.Aux[P,L])L
one error found
FAILURE: Build failed with an exception.
I wonder if there is a macro or another scala feature I can use to break this limitation, any advice?
I'm using scala 2.12.8 but can upgrade to 2.13 at any time.
If your goal is to generate an
HListlonger than 22 then there are plenty of waysBe careful, some of these calculations take long.
Also you can define
Product23,Tuple23although syntax sugar just with brackets will not work. The name of case class isn't significant, it can be
MyClassinstead ofTuple23.In Dotty there is TupleXXL.
If your goal is to write a method like
getHListyou can try to make it curriedor
Or you can divide long tuple into tuple of tuples and use deep
Generic(1 2 3 4).One more option is to write a whitebox macro
Since the macro is whitebox its return type will be proper,
Int :: Int :: ... :: HNil.Maybe the easiest is to use
shapeless.ProductArgsActually
ProductArgsare implemented in Shapeless viascala.Dynamicand whitebox macro.