Skip to content

Commit d74107f

Browse files
committed
Use _ to simplify some pattern matching code examples
1 parent 3301546 commit d74107f

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

docs/docs/reference/changed-features/match-syntax.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ The syntactical precedence of match expressions has been changed.
1111
```scala
1212
xs match {
1313
case Nil => "empty"
14-
case x :: xs1 => "nonempty"
14+
case _ => "nonempty"
1515
} match {
16-
case "empty" => 0
16+
case "empty" => 0
1717
case "nonempty" => 1
1818
}
1919
```
@@ -23,7 +23,7 @@ The syntactical precedence of match expressions has been changed.
2323
```scala
2424
xs match
2525
case Nil => "empty"
26-
case x :: xs1 => "nonempty"
26+
case _ => "nonempty"
2727
match
2828
case "empty" => 0
2929
case "nonempty" => 1
@@ -34,7 +34,7 @@ The syntactical precedence of match expressions has been changed.
3434
```scala
3535
if xs.match
3636
case Nil => false
37-
case _ => true
37+
case _ => true
3838
then "nonempty"
3939
else "empty"
4040
```
@@ -46,7 +46,7 @@ The syntactical precedence of match expressions has been changed.
4646

4747
The new syntax of match expressions is as follows.
4848

49-
```
49+
```ebnf
5050
InfixExpr ::= ...
5151
| InfixExpr MatchClause
5252
SimpleExpr ::= ...

docs/docs/reference/contextual/extension-methods.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,38 @@ extension [T: Numeric](x: T)
7676

7777
Type parameters on extensions can also be combined with type parameters on the methods
7878
themselves:
79+
7980
```scala
8081
extension [T](xs: List[T])
8182
def sumBy[U](f: T => U)(using Numeric[U]): U = ...
8283
```
8384

8485
Type arguments matching method type parameters are passed as usual:
86+
8587
```scala
8688
List("a", "bb", "ccc").sumBy[Int](_.length)
8789
```
90+
8891
By contrast, type arguments matching type parameters following `extension` can be passed
8992
only if the method is referenced as a regular method:
93+
9094
```scala
9195
List[String]("a", "bb", "ccc").sumBy(_.length)
9296
```
97+
9398
or, passing, both type arguments
99+
94100
```scala
95101
List[String]("a", "bb", "ccc").sumBy[Int](_.length)
96102
```
103+
97104
Extensions can also take using clauses. For instance, the `+` extension above could equivalently be written with a using clause:
98105

99106
```scala
100107
extension [T](x: T)(using n: Numeric[T])
101108
def + (y: T): T = n.plus(x, y)
102109
```
110+
103111
### Collective Extensions
104112

105113
Sometimes, one wants to define several extension methods that share the same
@@ -214,7 +222,7 @@ class List[T]:
214222
object List:
215223
...
216224
extension [T](xs: List[List[T]])
217-
def flatten: List[T] = xs.foldLeft(Nil: List[T])(_ ++ _)
225+
def flatten: List[T] = xs.foldLeft(List.empty[T])(_ ++ _)
218226

219227
given [T: Ordering]: Ordering[List[T]] with
220228
extension (xs: List[T])
@@ -276,7 +284,7 @@ def position(s: String)(ch: Char, n: Int): Int =
276284
Here are the syntax changes for extension methods and collective extensions relative
277285
to the [current syntax](../syntax.md).
278286

279-
```
287+
```ebnf
280288
BlockStat ::= ... | Extension
281289
TemplateStat ::= ... | Extension
282290
TopStat ::= ... | Extension

docs/docs/reference/enums/adts.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ enum Option[+T]:
6565

6666
def isDefined: Boolean = this match
6767
case None => false
68-
case some => true
68+
case _ => true
6969

7070
object Option:
7171

@@ -153,7 +153,7 @@ The changes are specified below as deltas with respect to the Scala syntax given
153153

154154
1. Enum definitions are defined as follows:
155155

156-
```
156+
```ebnf
157157
TmplDef ::= `enum' EnumDef
158158
EnumDef ::= id ClassConstr [`extends' [ConstrApps]] EnumBody
159159
EnumBody ::= [nl] ‘{’ [SelfType] EnumStat {semi EnumStat} ‘}’
@@ -163,7 +163,7 @@ The changes are specified below as deltas with respect to the Scala syntax given
163163
164164
2. Cases of enums are defined as follows:
165165
166-
```
166+
```ebnf
167167
EnumCase ::= `case' (id ClassConstr [`extends' ConstrApps]] | ids)
168168
```
169169

0 commit comments

Comments
 (0)