Skip to content

Commit 1b48a48

Browse files
authored
Merge pull request #2476 from sjrd/tabbed-code-tour-tuples
Enable tabbed Scala 2/3 code in tour/tuples.
2 parents 5bdc1d3 + 3f20027 commit 1b48a48

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

_tour/tuples.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,106 @@ Tuples are especially handy for returning multiple values from a method.
1818

1919
A tuple with two elements can be created as follows:
2020

21+
{% tabs tuple-construction %}
22+
23+
{% tab 'Scala 2 and 3' for=tuple-construction %}
2124
```scala mdoc
22-
val ingredient = ("Sugar" , 25)
25+
val ingredient = ("Sugar", 25)
2326
```
27+
{% endtab %}
2428

25-
This creates a tuple containing a `String` element and an `Int` element.
29+
{% endtabs %}
2630

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.
2932

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)`.
3234

3335
## Accessing the elements
3436

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.
3742

3843
```scala mdoc
3944
println(ingredient._1) // Sugar
4045
println(ingredient._2) // 25
4146
```
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 %}
4260

4361
## Pattern matching on tuples
4462

4563
A tuple can also be taken apart using pattern matching:
4664

65+
{% tabs tuple-extraction %}
66+
67+
{% tab 'Scala 2 and 3' for=tuple-extraction %}
4768
```scala mdoc
4869
val (name, quantity) = ingredient
49-
println(name) // Sugar
70+
println(name) // Sugar
5071
println(quantity) // 25
5172
```
73+
{% endtab %}
74+
75+
{% endtabs %}
5276

5377
Here `name`'s inferred type is `String` and `quantity`'s inferred type
5478
is `Int`.
5579

5680
Here is another example of pattern-matching a tuple:
5781

82+
{% tabs tuple-foreach-patmat %}
83+
84+
{% tab 'Scala 2 and 3' for=tuple-foreach-patmat %}
5885
```scala mdoc
5986
val planets =
6087
List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6),
6188
("Mars", 227.9), ("Jupiter", 778.3))
62-
planets.foreach{
89+
planets.foreach {
6390
case ("Earth", distance) =>
6491
println(s"Our planet is $distance million kilometers from the sun")
6592
case _ =>
6693
}
6794
```
95+
{% endtab %}
96+
97+
{% endtabs %}
6898

69-
Or, in `for` comprehension:
99+
Or, in a `for` comprehension:
70100

101+
{% tabs tuple-for-extraction class=tabs-scala-version %}
102+
103+
{% tab 'Scala 2' for=tuple-for-extraction %}
71104
```scala mdoc
72105
val numPairs = List((2, 5), (3, -7), (20, 56))
73106
for ((a, b) <- numPairs) {
74107
println(a * b)
75108
}
76109
```
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 %}
77121

78122
## Tuples and case classes
79123

0 commit comments

Comments
 (0)