Skip to content

Commit d68dbaf

Browse files
Add reference doc details
1 parent ee2f641 commit d68dbaf

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,6 +3171,7 @@ object Parsers {
31713171
val t = inSepRegion(InCase)(postfixExpr(Location.InGuard))
31723172
t.asSubMatch
31733173
case other =>
3174+
// the guard is reinterpreted as a sub-match when there is no leading IF or ARROW token
31743175
val t = grd1.asSubMatch
31753176
grd1 = EmptyTree
31763177
t

docs/_docs/reference/experimental/sub-cases.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ e.g., `case Some(x) => x.version match ...`.
3636
If none of the sub-cases succeed, then control flow returns to the outer match expression and proceeds as though the current case had not matched.
3737
For example, `Some(Document("...", Version.Stable(2, 1)))` matches the first pattern, but none of its sub-cases, and we therefore obtain the result `"unsupported"`.
3838

39-
More generally, sub-matches also allow:
40-
- Arbitrary nesting, e.g. sub-sub-matches are supported.
41-
- Interleaved boolean guards, e.g. `case Some(x: Int) if x != 0 if x match ...`.
42-
- Interleaving pattern extractors and computations for the scrutinees of sub-matches.
43-
4439

4540
## Motivation
4641

@@ -62,11 +57,21 @@ def version(d: Option[Document]) = d match
6257
case _ => "unsupported"
6358
```
6459

60+
## Details
6561

62+
Sub-cases allow:
63+
- Arbitrary nesting, e.g. sub-sub-matches are supported.
64+
- Interleaving boolean guards, e.g. `case Some(x: Int) if x != 0 if x match ...`.
65+
- Interleaving pattern extractors and computations for the scrutinees of sub-matches.
6666

67+
Sub-cases are supported for:
68+
- match clauses
69+
- catch clauses
70+
- partial functions
6771

72+
Similarly to catch clauses, match expressions with a single case can now be written on single line (without braces),
73+
e.g., `Some(1) match case Some(x) => x`.
6874

75+
Exhaustivity and reachability checking conservatively assume the sub-cases to be partial, similarly boolean guards.
6976

70-
71-
72-
77+
A sub-match is inlined iff the outer match is inlined, with the same semantics as the usual match expressions.

0 commit comments

Comments
 (0)