Skip to content

Commit f606a47

Browse files
committed
Add Product{1,2} supertrait to case classes
Case classes with arity <= 1 now also get a ProductN parent trait. This is necessary because we inherit productArity and Element methods in case classes from the ProdictN trait. Needed to add Product0 for this, which is not defined in Scala2.x.
1 parent c2cdd3a commit f606a47

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,12 @@ object desugar {
312312
def productConstr(n: Int) = {
313313
val tycon = ref(defn.ProductNClass(n).typeRef)
314314
val targs = constrVparamss.head map (_.tpt)
315-
AppliedTypeTree(tycon, targs)
315+
if (targs.isEmpty) tycon else AppliedTypeTree(tycon, targs)
316316
}
317317

318318
// Case classes get a ProductN parent
319319
var parents1 = parents
320-
if ((mods is Case) && 2 <= arity && arity <= Definitions.MaxTupleArity)
320+
if ((mods is Case) && arity <= Definitions.MaxTupleArity)
321321
parents1 = parents1 :+ productConstr(arity)
322322

323323
// The thicket which is the desugared version of the companion object

src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ class Definitions {
394394
lazy val Function0_apply = FunctionClass(0).requiredMethod(nme.apply)
395395

396396
lazy val TupleClass = mkArityArray("scala.Tuple", MaxTupleArity, 2)
397-
lazy val ProductNClass = mkArityArray("scala.Product", MaxTupleArity, 2)
397+
lazy val ProductNClass = mkArityArray("scala.Product", MaxTupleArity, 0)
398398

399399
lazy val FunctionClasses: Set[Symbol] = FunctionClass.toSet
400400
lazy val TupleClasses: Set[Symbol] = TupleClass.toSet

src/scala/Product0.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2002-2013, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
package scala
9+
10+
/** A class for Product0 which was missing from the scala distribution. */
11+
object Product0 {
12+
def unapply(x: Product0): Option[Product0] =
13+
Some(x)
14+
}
15+
16+
trait Product0 extends Any with Product {
17+
18+
override def productArity = 0
19+
20+
@throws(classOf[IndexOutOfBoundsException])
21+
override def productElement(n: Int) =
22+
throw new IndexOutOfBoundsException(n.toString())
23+
}
24+

0 commit comments

Comments
 (0)