In scala I have an algebraic type I would like to work with:
case class Person(name: String, age: Int, weight: Float)
A library I do not control is providing me values via an enumeration of String values:
Enumeration(("name", "Bob"), ("age", "32"), ("weight", "45.64"))
Enumeration(("age", "23"), ("weight", "20.0"), ("name", "Alice"))
What's the most "scala-ish" way to get the annoying (name,value) string pairs into my case class?
I've only thought of the obvious iterative CRUD method:
def toClass(e: Enumeration[(String,String)]): Person = {
var name: String = _
...
for(pair <- e) pair._1 match {
"name" => name = pair._2
}
Person(name, ...)
}