Search code examples
scalafunctional-programmingcase-class

How to read from a case class in Scala?


I am new to Scala. I have a case class. The code is given below.

case class ReportInfoPosted(
  name:  Option[String],
  id:    Option[String],
  order: Option[Int]
)

I also have a function which returns an seq of objects of the class. This is what is being returned.

ReportInfoPosted(Some(Sales Dollars),Some(4e6d8ec1-4c00-4193-be15-2fa0509849a7),Some(0))

Now I want to read values from the object. I have looked at some resources on the web, this is what I have tried.

for(el <- reportlist){
    println(el.input)
}

for(el <- reportlist){
    println(el.id)
}

BTW, reportlist is the seq of obejcts. None are working. I don't know what to do.


Solution

  • Your question is pretty vague. Do you just mean this?

    val a = ReportInfoPosted(Some("a"), Some("a"), Some(1))
    val b = ReportInfoPosted(Some("b"), Some("b"), Some(2))
    val reportlist: Seq[ReportInfoPosted] = Seq(a,b)
    
    for (report <- reportlist) {
      println(report.name)
    }
    

    prints:

    Some(a)
    Some(b)