如何从 Scala 中的案例 class 中读取?

How to read from a case class in Scala?

我是 Scala 新手。我有一个案例class。代码如下。

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

我还有一个函数,其中 returns seq class 的对象。这是返回的内容。

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

现在我想从对象中读取值。我看了网上的一些资源,这是我试过的。

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

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

顺便说一句,reportlist 是对象的 seq。 None 正在工作。我不知道该怎么办。

你的问题很含糊。你是这个意思吗?

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)
}

打印:

Some(a)
Some(b)