Skip to content

Commit 1d4c392

Browse files
committed
Add section on auto-tupling of function parameters
1 parent 79e6b51 commit 1d4c392

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
layout: doc-page
3+
title: "Automatic Tupling of Function Parameters"
4+
---
5+
6+
Say you have a list of pairs
7+
8+
val xs: List[(Int, Int)]
9+
10+
and you want to map `xs` to a list of `Int`s so that eich pair of numbers is mapped to
11+
their sum. Previously, the best way to do this was with a pattern-matching decomposition:
12+
13+
xs map {
14+
case (x, y) => x + y
15+
}
16+
17+
While correct, this is also inconvenient. Dotty now also allows:
18+
19+
xs.map {
20+
(x, y) => x + y
21+
}
22+
23+
or, equivalently:
24+
25+
xs.map(_ + _)
26+
27+
Generally, a function value with `n > 1` parameters is converted to a
28+
pattern-matching closure using `case` if the expected type is a unary
29+
function type of the form `((T_1, ..., T_n)) => U`.
30+
31+
32+
33+

docs/docs/reference/desugarEnums.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ comma separated simple cases into a sequence of cases.
8989

9090
case C <params> ...
9191

92-
expands analogous to a case class:
92+
expands analogous to a final case class:
9393

9494
final case class C <params> ...
9595

docs/sidebar.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ sidebar:
2020
-title: Multiversal Equality
2121
url: docs/reference/multiversal-equality.html
2222
- title: By-Name Implicits
23-
url: docs/reference/implicit-by-name-parameters.html
23+
url: docs/reference/implicit-by-name-parameters.htmldocs/reference/implicit-by-name-parameters.html
24+
- title: Auto Parameter Tupling
25+
url: docs/reference/auto-parameter-tupling.html
2426
- title: Usage
2527
subsection:
2628
- title: sbt-projects
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package autoParamTupling {
2+
3+
object t1 {
4+
val xs: List[(Int, Int)] = ???
5+
6+
xs.map {
7+
case (x, y) => x + y
8+
}
9+
10+
xs.map {
11+
(x, y) => x + y
12+
}
13+
14+
xs.map(_ + _)
15+
16+
}
17+
}

0 commit comments

Comments
 (0)