Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala case class arguments instantiation from array

Consider a case class with a possibly large number of members; to illustrate the case assume two arguments, as in

case class C(s1: String, s2: String)

and therefore assume an array with size of at least that many arguments,

val a = Array("a1", "a2")

Then

scala> C(a(0), a(1))
res9: C = c(a1,a2)

However, is there an approach to case class instantiation where there is no need to refer to each element in the array for any (possibly large) number of predefined class members ?

like image 858
elm Avatar asked Mar 07 '14 08:03

elm


2 Answers

No, you can't. You cannot guarantee your array size is at least the number of members of your case class.

You can use tuples though.

Suppose you have a mentioned case class and a tuple that looks like this:

val t = ("a1", "a2")

Then you can do:

c.tupled(t)
like image 92
serejja Avatar answered Sep 27 '22 21:09

serejja


Having gathered bits and pieces from the other answers, a solution that uses Shapeless 2.0.0 is thus as follows,

import shapeless._
import HList._
import syntax.std.traversable._

val a = List("a1", 2)                    // List[Any]
val aa = a.toHList[String::Int::HNil]
val aaa = aa.get.tupled                  // (String, Int)

Then we can instantiate a given case class with

case class C(val s1: String, val i2: Int)
val ins = C.tupled(aaa)

and so

scala> ins.s1
res10: String = a1

scala> ins.i2
res11: Int = 2

The type signature of toHList is known at compile time as much as the case class members types to be instantiate onto.

like image 26
elm Avatar answered Sep 27 '22 21:09

elm