This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Case classes and unapply

1 reply
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Dear all,is it possible, when you have a case class already instanciated (for example because of a filter on a collection of case classes) to extract the fields?
val (myfield1,myfield2,myfield3) = MyCaseClassInstance.something ?

Best Regards
Edmondo
Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Case classes and unapply

Just follow exactly the same patterns that you would in a match block...
val (myfield1, myfield2, myfield3) = myTupleInstance val myfield1 :: myfield2 :: myfield3 :: Nil = myListInstanceval Array(myfield1,myfield2,myfield3) = myArrayInstanceval MyCaseClass(myfield1,myfield2,myfield3) = myCaseClassInstance


On 20 January 2012 15:40, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Dear all,is it possible, when you have a case class already instanciated (for example because of a filter on a collection of case classes) to extract the fields?
val (myfield1,myfield2,myfield3) = MyCaseClassInstance.something ?

Best Regards
Edmondo


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland