Unpacking tuple directly into class in scala

Viewed 1918

Scala gives the ability to unpack a tuple into multiple local variables when performing various operations, for example if I have some data

val infos = Array(("Matt", "Awesome"), ("Matt's Brother", "Just OK"))

then instead of doing something ugly like

infos.map{ person_info => person_info._1 + " is " + person_info._2 }

I can choose the much more elegant

infos.map{ case (person, status) => person + " is " + status }

One thing I've often wondered about is how to directly unpack the tuple into, say, the arguments to be used in a class constructor. I'm imagining something like this:

case class PersonInfo(person: String, status: String)
infos.map{ case (p: PersonInfo) => p.person + " is " + p.status }

or even better if PersonInfo has methods:

infos.map{ case (p: PersonInfo) => p.verboseStatus() }

But of course this doesn't work. Apologies if this has already been asked -- I haven't been able to find a direct answer -- is there a way to do this?

5 Answers
Related