@@ -18,62 +18,106 @@ Tuples are especially handy for returning multiple values from a method.
18
18
19
19
A tuple with two elements can be created as follows:
20
20
21
+ {% tabs tuple-construction %}
22
+
23
+ {% tab 'Scala 2 and 3' for=tuple-construction %}
21
24
``` scala mdoc
22
- val ingredient = (" Sugar" , 25 )
25
+ val ingredient = (" Sugar" , 25 )
23
26
```
27
+ {% endtab %}
24
28
25
- This creates a tuple containing a ` String ` element and an ` Int ` element.
29
+ {% endtabs %}
26
30
27
- The inferred type of ` ingredient ` is ` (String, Int) ` , which is shorthand
28
- for ` Tuple2[String, Int] ` .
31
+ This creates a tuple containing a ` String ` element and an ` Int ` element.
29
32
30
- To represent tuples, Scala uses a series of classes: ` Tuple2 ` , ` Tuple3 ` , etc., through ` Tuple22 ` .
31
- Each class has as many type parameters as it has elements.
33
+ The inferred type of ` ingredient ` is ` (String, Int) ` .
32
34
33
35
## Accessing the elements
34
36
35
- One way of accessing tuple elements is by position. The individual
36
- elements are named ` _1 ` , ` _2 ` , and so forth.
37
+ {% tabs tuple-indexed-access class=tabs-scala-version %}
38
+
39
+ {% tab 'Scala 2' for=tuple-indexed-access %}
40
+ One way of accessing tuple elements is their positions.
41
+ The individual elements are named ` _1 ` , ` _2 ` , and so forth.
37
42
38
43
``` scala mdoc
39
44
println(ingredient._1) // Sugar
40
45
println(ingredient._2) // 25
41
46
```
47
+ {% endtab %}
48
+
49
+ {% tab 'Scala 3' for=tuple-indexed-access %}
50
+ One way of accessing tuple elements is their positions.
51
+ The individual elements are accessed with ` tuple(0) ` , ` tuple(1) ` , and so forth.
52
+
53
+ ``` scala
54
+ println(ingredient(0 )) // Sugar
55
+ println(ingredient(1 )) // 25
56
+ ```
57
+ {% endtab %}
58
+
59
+ {% endtabs %}
42
60
43
61
## Pattern matching on tuples
44
62
45
63
A tuple can also be taken apart using pattern matching:
46
64
65
+ {% tabs tuple-extraction %}
66
+
67
+ {% tab 'Scala 2 and 3' for=tuple-extraction %}
47
68
``` scala mdoc
48
69
val (name, quantity) = ingredient
49
- println(name) // Sugar
70
+ println(name) // Sugar
50
71
println(quantity) // 25
51
72
```
73
+ {% endtab %}
74
+
75
+ {% endtabs %}
52
76
53
77
Here ` name ` 's inferred type is ` String ` and ` quantity ` 's inferred type
54
78
is ` Int ` .
55
79
56
80
Here is another example of pattern-matching a tuple:
57
81
82
+ {% tabs tuple-foreach-patmat %}
83
+
84
+ {% tab 'Scala 2 and 3' for=tuple-foreach-patmat %}
58
85
``` scala mdoc
59
86
val planets =
60
87
List ((" Mercury" , 57.9 ), (" Venus" , 108.2 ), (" Earth" , 149.6 ),
61
88
(" Mars" , 227.9 ), (" Jupiter" , 778.3 ))
62
- planets.foreach{
89
+ planets.foreach {
63
90
case (" Earth" , distance) =>
64
91
println(s " Our planet is $distance million kilometers from the sun " )
65
92
case _ =>
66
93
}
67
94
```
95
+ {% endtab %}
96
+
97
+ {% endtabs %}
68
98
69
- Or, in ` for ` comprehension:
99
+ Or, in a ` for ` comprehension:
70
100
101
+ {% tabs tuple-for-extraction class=tabs-scala-version %}
102
+
103
+ {% tab 'Scala 2' for=tuple-for-extraction %}
71
104
``` scala mdoc
72
105
val numPairs = List ((2 , 5 ), (3 , - 7 ), (20 , 56 ))
73
106
for ((a, b) <- numPairs) {
74
107
println(a * b)
75
108
}
76
109
```
110
+ {% endtab %}
111
+
112
+ {% tab 'Scala 3' for=tuple-for-extraction %}
113
+ ``` scala
114
+ val numPairs = List ((2 , 5 ), (3 , - 7 ), (20 , 56 ))
115
+ for (a, b) <- numPairs do
116
+ println(a * b)
117
+ ```
118
+ {% endtab %}
119
+
120
+ {% endtabs %}
77
121
78
122
## Tuples and case classes
79
123
0 commit comments