【发布时间】:2013-12-26 07:43:12
【问题描述】:
作为后续 Matt R's question,由于 Scala 2.10 已经发布了很长时间,提取案例类的字段和值的最佳方法是什么。举个类似的例子:
case class Colour(red: Int, green: Int, blue: String) {
val other: Int = 42
}
val RBG = Colour(1,3,"isBlue")
我想获得一个列表(或数组或任何迭代器),它将在构造函数中声明为元组值的字段,如下所示:
[(red, 1),(green, 3),(blue, "isBlue")]
我知道网上有很多关于同一问题的示例,但正如我所说,我想知道提取所需信息的最理想方法是什么
【问题讨论】:
-
因为案例类实现了 Product 特征,您应该能够使用 Product 方法:productIterator 与 zipwithindex 结合使用。
标签: scala reflection introspection scala-2.10