看起来,如果案例类延伸Iterable[T]
,则toString
方法发生更改。案例类toString与Iterable特征纠缠?
case class MyPoint(x: Int, y: Int)
case class MyOtherPoint(x: Int, y: Int) extends Iterable[Double] {
def iterator: Iterator[Double] = Iterator.fill(4)(1.0)
}
object Main extends App {
val my_pt = MyPoint(4,5)
println(my_pt) // MyPoint(4,5)
// println(my_pt.iterator) // ERROR, iterator is not a member of MyPoint
val my_other_pt = MyOtherPoint(4, 5)
println(my_other_pt) // MyOtherPoint(1.0, 1.0, 1.0, 1.0)
println(my_other_pt.productIterator.toList) // List(4, 5)
}
这似乎是相当不幸的,特别是考虑到虽然case类默认扩展Product
,从而有productIterator
,他们不是想延长Iterable
。
这是Scala编译器中的错误吗?