Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

unpacking case class

object HelloWorld {
   
   sealed case class MyRec(a: Int, b: Double, c: String)
   
   def main(args: Array[String]) {
      val rec = MyRec(1, 4.2, "hello")
      MyRec.unapply(rec) match { // unpacks MyRec
          case Some(fields) =>   // into a tuple of individual fields
              fields.productIterator.foreach(x => x match {
                  case y: Int => println(s"integer $y")
                  case y: Double => println(s"double $y")
                  case y: String => println(s"string $y")
              })
          case None => println("impossible!")
      }
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.