I want to define a function that is parameterized by the generic type of the product, but which can solve the Product problem. Here is an example fragment. I would like to do an arity check when f (...) is called, and not when f (...) () is called. How can i do this?
def f[T<:Product](names:Seq[String], values:()=>T) = {
() => {
val x = values()
if (x.productArity != names.size) scala.sys.error("Size mismatch")
names.zip(x.productIterator.map(_.toString).toSeq).map(kv => kv._1+"="+kv._2)
}
}
(This is a rather useless function, just for demonstration. The important points are: (1) it is parameterized by the Product type; (2) the function makes sense only if the arity of the product corresponds to a certain value, which when I call the function, (3) to me Itβs expensive or impossible to get an instance of the Product when I call the function. My actual use case is a utility class for writing SQL statements based on spark RDD.)
If necessary, I could write a whole family of functions, one for each Tuple size. But this is unpleasant, and I hope for a better solution.
source
share