【发布时间】:2021-11-06 19:25:24
【问题描述】:
使用 scala 3,您可以使用 Tuple.fromProductTyped 从案例类中获取元组。
我想获取嵌套案例类的所有字段,例如:
case class Baz(x: String)
case class Employee(name: Baz, number: Int, manager: Boolean)
Tuple.fromProductTyped(Employee(Baz("hello"), 42, false)) // (Baz(hello),42,false) but I need (hello,42,false)
下面的代码尝试获取嵌套案例类的元组,但在第 *** 行失败
no implicit argument of type deriving.Mirror.ProductOf[Product] was found for parameter m of method fromProductTyped in object Tuple
val tuple2 = Tuple.fromProductTyped(x)
trait RowEncoder[A] {
def encodeRow(a: A): List[String]
}
def tupleToCsv[A <: Tuple: RowEncoder](tuple: A): List[String] = summon[RowEncoder[A]].encodeRow(tuple)
case class Baz(x: String)
case class Employee(name: Baz, number: Int, manager: Boolean)
def flatTuple[A <: Tuple](tuple: A): Tuple = {
tuple match {
case x *: xs =>
x match {
case x: Product =>
val tuple2 = Tuple.fromProductTyped(x) // ***
flatTuple(tuple2) ++ flatTuple(xs)
case x => x *: flatTuple(xs)
}
case _ => EmptyTuple
}
}
val tuple = Tuple.fromProductTyped(Employee(Baz("hello"), 42, false))
println(flatTuple(tuple)) // expected: (hello,42,false)
要测试在第 *** 行将 x 替换为 Baz("hello")
我该如何解决?
【问题讨论】: