File tree Expand file tree Collapse file tree 4 files changed +54
-2
lines changed Expand file tree Collapse file tree 4 files changed +54
-2
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change @@ -89,7 +89,7 @@ comma separated simple cases into a sequence of cases.
89
89
90
90
case C <params> ...
91
91
92
- expands analogous to a case class:
92
+ expands analogous to a final case class:
93
93
94
94
final case class C <params> ...
95
95
Original file line number Diff line number Diff line change @@ -20,7 +20,9 @@ sidebar:
20
20
-title : Multiversal Equality
21
21
url : docs/reference/multiversal-equality.html
22
22
- 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
24
26
- title : Usage
25
27
subsection :
26
28
- title : sbt-projects
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments