scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.88k stars 1.06k forks source link

Document type Aliases can be used as an unapply pattern #9305

Open bishabosha opened 4 years ago

bishabosha commented 4 years ago

from commit 6be96724899f9c34db2e6c2617534dc9c4a15528:

Following type aliases when pattern matching. Faced with a pattern like Apply(x, xs) we first look for an Apply object which is an extractor. If this fails, we now interprete Apply as a type. If it is a type alias which points to a class type that has a companion module, we then try that companion module as an extractor. Scala 2.x does ot that way, and it's used widely within dotty itself. Think tpd.Apply as the found object, Trees.Apply as the extractor. Also, added a fix to normalization which made normalization go deep into a method type.

found by @smarter https://github.com/lampepfl/dotty/blob/7723864d3669cea8bba7481d3b62eaf7e560c2bf/compiler/src/dotty/tools/dotc/typer/Applications.scala#L1198-L1200

Example

scala> case class Foo(x: Int)
// defined case class Foo

scala> object inst:
     |   type FooAlias = Foo
     | 
// defined object inst

scala> def foo(t: inst.FooAlias) = t match
     |   case inst.FooAlias(x) => x
     | 
def foo(t: inst.FooAlias): Int
liufengyun commented 3 years ago

If the feature is not important in practice, I'd vote to remove it and align with Scala 2.