From 4bdad3c21a1461bed6e91ef69dd767fa5211f60d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 14 Feb 2017 09:45:44 +0100 Subject: Change return type of `apply`. In an enum case like case C() extends P1 with ... with Pn ... apply now returns `P1 & ... & Pn`, where before it was just P1. Also, add to Option test. --- compiler/src/dotty/tools/dotc/ast/Desugar.scala | 14 +++++++------- tests/run/enum-Option.scala | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index aa073429d..575af97f4 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -344,13 +344,6 @@ object desugar { // new C[Ts](paramss) lazy val creatorExpr = New(classTypeRef, constrVparamss nestedMap refOfDef) - // The return type of the `apply` and `copy` methods - val applyResultTpt = - if (isEnumCase) - if (parents.isEmpty) enumClassTypeRef - else parents.head - else TypeTree() - // Methods to add to a case class C[..](p1: T1, ..., pN: Tn)(moreParams) // def isDefined = true // def productArity = N @@ -436,6 +429,13 @@ object desugar { // For all other classes, the parent is AnyRef. val companions = if (isCaseClass) { + // The return type of the `apply` method + val applyResultTpt = + if (isEnumCase) + if (parents.isEmpty) enumClassTypeRef + else parents.reduceLeft(AndTypeTree) + else TypeTree() + val parent = if (constrTparams.nonEmpty || constrVparamss.length > 1 || diff --git a/tests/run/enum-Option.scala b/tests/run/enum-Option.scala index 74e449daf..b9efadf0d 100644 --- a/tests/run/enum-Option.scala +++ b/tests/run/enum-Option.scala @@ -2,6 +2,7 @@ enum class Option[+T] extends Serializable { def isDefined: Boolean } object Option { + def apply[T](x: T): Option[T] = if (x == null) None else Some(x) case Some(x: T) { def isDefined = true } @@ -11,6 +12,8 @@ object Option { } object Test { - def main(args: Array[String]) = + def main(args: Array[String]) = { assert(Some(None).isDefined) + Option(22) match { case Option.Some(x) => assert(x == 22) } + } } -- cgit v1.2.3