Last Updated: February 25, 2016
·
3.471K
· eugener

Scala case class "reflection"

Case classes automatically mix in the Product trait, providing untyped, indexed access to the fields without any reflection:

case class Person(name: String, age: Int)

val p = Person("Eugene", 45)
val name = p.productElement(0) // name = "Eugene": Any
val age = p.productElement(1) // age = 45: Any
val fields = p.productIterator.toList // fields = List[Any]("Eugene", 45)