目前我使用scalaz和shapeless来生成一个只更新更改值的case类,但是对于某些扩展功能我需要知道哪些字段(名称)发生了变化,有没有简单的方法呢?
或者通常有更好的方法来检查每个字段是否相等?
示例:
case class A(quanity: Option[Long], lose: Option[Long])
val a0 = A(Option(50), Option(10))
val a1 = A(Option(50), Option(20))
// Here it will be merged to a2 which is done by scalaz
// Now I need the fields that changed, ie. quanity or lose
// (or any other field if there are more
// a list or anything would be good enough:
val mergedList: List[String] = "lose" :: List()
答案 0 :(得分:0)
首先想到的是 - productIterator:
scala> A(5, "asd")
res6: A = A(5,asd)
scala> A(5, "asd")
res7: A = A(5,asd)
scala> A(1, "asd")
res8: A = A(1,asd)
scala> res6.productIterator.toList
res9: List[Any] = List(5, asd)
scala> res7.productIterator.toList
res10: List[Any] = List(5, asd)
scala> res8.productIterator.toList
res11: List[Any] = List(1, asd)
scala> res9 == res10
res12: Boolean = true
scala> res9 == res11
res13: Boolean = false
scala> res11.diff(res10)
res18: List[Any] = List(1)