From 944e677f437c39d85280c388cab000b5490e4386 Mon Sep 17 00:00:00 2001 From: Olivier Blanvillain Date: Thu, 6 Apr 2017 18:35:20 +0200 Subject: Decouple Product and pattern-matching Product pattern use to: - have a `<: Product` requirement - compute the arity of a pattern by looking at `N` in a `ProductN` superclass. This commit changes `<: Product`, instead we look for a `_1` member. The arity is determined by inspecting `_1` to `_N` members instead. --- Here another attempt to formalize Dotty's pattern-matching (base on #1805). This covers the *Extractor Patterns* [section of the spec](https://www.scala-lang.org/files/archive/spec/2.12/08-pattern-matching.html#extractor-patterns). Dotty support 4 different extractor patterns: Boolean Pattern, Product Pattern, Seq Pattern and Name Based Pattern. Boolean Pattern - Extractor defines `def unapply(x: T): Boolean` - Pattern-matching on exactly `0` patterns Product Pattern - Extractor defines `def unapply(x: T): U` - `N > 0` is the maximum number of consecutive (parameterless `def` or `val`) `_1: P1` ... `_N: PN` members in `U` - Pattern-matching on exactly `N` patterns with types `P1, P2, ..., PN` Seq Pattern - Extractor defines `def unapplySeq(x: T): U` - `U` has (parameterless `def` or `val`) members `isEmpty: Boolean` and `get: S` - `S <: Seq[V]` - Pattern-matching on any number of pattern with types `V, V, ..., V` Name Based Pattern - Extractor defines `def unapply(x: T): U` - `U` has (parameterless `def` or `val`) members `isEmpty: Boolean` and `get: S` - If there is exactly `1` pattern, pattern-matching on `1` pattern with type `S` - Otherwise fallback to Product Pattern on type `U` In case of ambiguities, *Product Pattern* is preferred over *Name Based Pattern*. --- compiler/src/dotty/tools/dotc/core/Definitions.scala | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'compiler/src/dotty/tools/dotc/core/Definitions.scala') diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 337f48064..bc55ce638 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -10,6 +10,7 @@ import scala.collection.{ mutable, immutable } import PartialFunction._ import collection.mutable import util.common.alwaysZero +import typer.Applications object Definitions { @@ -847,17 +848,9 @@ class Definitions { } def isProductSubType(tp: Type)(implicit ctx: Context) = - (tp derivesFrom ProductType.symbol) && tp.baseClasses.exists(isProductClass) + Applications.extractorMemberType(tp, nme._1).exists - def productArity(tp: Type)(implicit ctx: Context) = - if (tp derivesFrom ProductType.symbol) - tp.baseClasses.find(isProductClass) match { - case Some(prod) => prod.typeParams.length - case None => -1 - } - else -1 - - /** Is `tp` (an alias) of either a scala.FunctionN or a scala.ImplicitFunctionN ? */ + /** Is `tp` (an alias) of either a scala.FunctionN or a scala.ImplicitFunctionN? */ def isFunctionType(tp: Type)(implicit ctx: Context) = { val arity = functionArity(tp) val sym = tp.dealias.typeSymbol -- cgit v1.2.3