Skip to content

Commit bdde62c

Browse files
Sketch pattern matching documentation
1 parent 37db4e9 commit bdde62c

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

docs/docs/index.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ layout: doc-page
33
title: "Dotty Documentation"
44
---
55

6-
Dotty is a platform to try out new language concepts and compiler technologies for Scala.
7-
The focus is mainly on simplification. We remove extraneous syntax (e.g. no XML literals),
8-
and try to boil down Scala’s types into a smaller set of more fundamental constructors.
6+
Dotty is a platform to try out new language concepts and compiler technologies for Scala.
7+
The focus is mainly on simplification. We remove extraneous syntax (e.g. no XML literals),
8+
and try to boil down Scala’s types into a smaller set of more fundamental constructors.
99
The theory behind these constructors is researched in DOT, a calculus for dependent object types.
1010

1111
In this documentation you will find information on how to use the Dotty compiler on your machine, navigate through
@@ -22,6 +22,7 @@ Contents
2222
- [Algebraic Data Types](reference/adts.md)
2323
- [Enum Translation](reference/desugarEnums.md)
2424
- [By-Name Implicits](reference/implicit-by-name-parameters.md)
25+
- [Option-less pattern matching](reference/pattern-matching.md)
2526
* Usage
2627
- [Migrating from Scala 2](usage/migrating.md): migration information
2728
- [Dotty projects with sbt](usage/sbt-projects.md): using sbt
@@ -32,7 +33,7 @@ Contents
3233
- [Workflow](contributing/workflow.md): common dev patterns and hints
3334
- [Eclipse](contributing/eclipse.md): setting up dev environment
3435
- [Intellij-IDEA](contributing/intellij-idea.md): setting up dev environment
35-
- [Working with the Backend](contributing/backend.md): working with the scala backend
36+
- [Working with the Backend](contributing/backend.md): working with the scala backend
3637
* Internals document the compiler internals
3738
- [Syntax Summary](internals/syntax.md): brief analysis of the syntax
3839
- [Project Structure](internals/overall-structure.md): of the project
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
layout: doc-page
3+
title: "Option-less pattern matching"
4+
---
5+
6+
Dotty pattern matching supports the following [extractors](https://www.scala-lang.org/files/archive/spec/2.13/08-pattern-matching.html#extractor-patterns):
7+
8+
Boolean Pattern
9+
10+
- Extractor defines `def unapply(x: T): Boolean`
11+
- Pattern-matching on exactly `0` patterns
12+
13+
Product Pattern
14+
15+
- Extractor defines `def unapply(x: T): U`
16+
- `U <: Product`
17+
- `N > 0` is the maximum number of consecutive (parameterless `def` or `val`) `_1: P1` ... `_N: PN` members in `U`
18+
- Pattern-matching on exactly `N` patterns with types `P1, P2, ..., PN`
19+
20+
Seq Pattern
21+
22+
- Extractor defines `def unapplySeq(x: T): U`
23+
- `U` has (parameterless `def` or `val`) members `isEmpty: Boolean` and `get: S`
24+
- `S <: Seq[V]`
25+
- Pattern-matching on any number of pattern with types `V, V, ..., V`
26+
27+
Name Based Pattern
28+
29+
- Extractor defines `def unapply(x: T): U`
30+
- `U` has (parameterless `def` or `val`) members `isEmpty: Boolean` and `get: S`
31+
- If there is exactly `1` pattern, pattern-matching on `1` pattern with type `S`
32+
- Otherwise `N > 0` is the maximum number of consecutive (parameterless `def` or `val`) `_1: P1` ... `_N: PN` members in `U`
33+
- Pattern-matching on exactly `N` patterns with types `P1, P2, ..., PN`
34+
35+
In case of ambiguities, *Product Pattern* is preferred over *Name Based Pattern*.
36+
37+
Dotty pattern matching is not special cased to case classes. Instead, allocation of Tuples and Options is avoided by desugaring case classes to use a *Product Pattern* extractor:
38+
39+
```scala
40+
case class CC(i: Int, s: String)
41+
```
42+
43+
becomes
44+
45+
```scala
46+
class CC(val i: Int, val s: String) extends Product { ... }
47+
48+
object {
49+
def apply(i: Int, s: String): CC = new CC(i, s)
50+
def unapply(cc: CC): CC = cc
51+
}
52+
```

docs/sidebar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ sidebar:
1717
url: docs/reference/desugarEnums.html
1818
- title: By-Name Implicits
1919
url: docs/reference/implicit-by-name-parameters.html
20+
- title: Option-less pattern matching
21+
url: docs/reference/pattern-matching.html
2022
- title: Usage
2123
subsection:
2224
- title: sbt-projects

0 commit comments

Comments
 (0)