after I learned that case classes extend Product, I wondered why they do not extend ProductN. E.g., given a code like:
case class Foo(a: Int)
I'd expect Foo(1).asInstanceOf[Product1[Int]]
to work, but it does not (checked with Scala 2.9.1, and confirmed by other sources and by Product
documentation).
I was interested in this, because I wanted to declare classes such as:
abstract class UnaryOp[T1 <: Exp[_], R](t1: T1) extends Exp[R] {
this: Product1[T1] =>
}
This way, a node for a Unary operation must be implement Product1. It would be nice if being simply a case class with one parameter would be enough for this.
Consider this:
case class X(n: Int)
case class Y(x: String, y: Int) extends X(y)
If case classes extended ProductN
, then that would extend both Product1
and Product2
, but the type parameter changes, so there are two different overloads for _1
. This is just one problem -- I bet there are others.
Now, case class inheriting case class has been deprecated, and Martin Odersky is now considering making them inherit ProductN. AFAIK, is has not been done yet, but the obstacle has been removed.
I brought it back shortly after martin said we could do it. It doesn't work right yet, but to the extent that it does, it's behind -Xexperimental in trunk builds.
scala> case class Foo[T, U](x1: T, x2: U)
defined class Foo
scala> Foo(List("a"), "b")
res0: Foo[List[java.lang.String],java.lang.String] = Foo(List(a),b)
scala> res0.isInstanceOf[Product2[_,_]]
res1: Boolean = true
If Product1[Int]
would have been automatically extended the val _1: Int
would also have to be provided. Although I could imagine, that it could be automated that a
gets assigned to _1
etc etc, but it is not. Probably just not to make things even more complicated.
来源:https://stackoverflow.com/questions/7338788/why-do-case-classes-extend-only-product-and-not-product1-product2-product