Skip to content

Commit a613231

Browse files
authored
Merge pull request scala#1268 from SethTisue/pr-1266-followup
language tour: overhaul tuples section
2 parents 03df5ab + 6ba8aa4 commit a613231

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

_tour/tuples.md

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,82 +14,71 @@ topics: tuples
1414
redirect_from: "/tutorials/tour/tuples.html"
1515
---
1616

17-
In Scala, a tuple is a class that can hold elements of different types.
18-
Tuples are immutable.
17+
In Scala, a tuple is a value that contains a fixed number of elements, each
18+
with a distinct type. Tuples are immutable.
1919

20-
Tuples come in handy when we have to return multiple values from a function.
20+
Tuples are especially handy for returning multiple values from a method.
2121

22-
A tuple can be created as:
22+
A tuple with two elements can be created as follows:
2323

2424
```tut
25-
val ingredient = ("Sugar" , 25):Tuple2[String, Int]
25+
val ingredient = ("Sugar" , 25)
2626
```
27-
This creates a tuple containing a String element and an Int element.
2827

29-
Tuple in Scala is a series of classes: Tuple2, Tuple3, etc., through Tuple22.
30-
So when we create a tuple with n elements(n lying between 2 and 22), Scala basically instantiates
31-
one of the corresponding classes from the group, parameterized with types of constituent elements.
32-
For eg., ingredient is of type Tuple2[String, Int].
28+
This creates a tuple containing a `String` element and an `Int` element.
29+
30+
The inferred type of `ingredient` is `(String, Int)`, which is shorthand
31+
for `Tuple2[String, Int]`.
32+
33+
To represent tuples, Scala uses a series of classes: `Tuple2`, `Tuple3`, etc., through `Tuple22`.
34+
Each class has as many type parameters as it has elements.
3335

3436
## Accessing the elements
3537

36-
Tuple elements are accessed using underscore syntax.
37-
'tuple._n' gives nth element(given there are that many elements).
38+
One way of accessing tuple elements is by position. The individual
39+
elements are named `_1`, `_2`, and so forth.
3840

3941
```tut
4042
println(ingredient._1) // Sugar
41-
4243
println(ingredient._2) // 25
4344
```
4445

45-
## Destructuring tuple data
46+
## Pattern matching on tuples
4647

47-
Scala tuple also supports destructuring.
48+
A tuple can also be taken apart using pattern matching:
4849

4950
```tut
5051
val (name, quantity) = ingredient
51-
5252
println(name) // Sugar
53-
5453
println(quantity) // 25
5554
```
5655

57-
Tuple destructuring can be used in pattern matching too.
56+
Here `name`'s inferred type is `String` and `quantity`'s inferred type
57+
is `Int`.
58+
59+
Here is another example of pattern-matching a tuple:
5860

5961
```tut
60-
val planetDistanceFromSun = List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6 ), ("Mars", 227.9), ("Jupiter", 778.3))
61-
62-
planetDistanceFromSun.foreach{ tuple => {
63-
64-
tuple match {
65-
66-
case ("Mercury", distance) => println(s"Mercury is $distance millions km far from Sun")
67-
68-
case p if(p._1 == "Venus") => println(s"Venus is ${p._2} millions km far from Sun")
69-
70-
case p if(p._1 == "Earth") => println(s"Blue planet is ${p._2} millions km far from Sun")
71-
72-
case _ => println("Too far....")
73-
74-
}
75-
76-
}
77-
62+
val planets =
63+
List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6),
64+
("Mars", 227.9), ("Jupiter", 778.3))
65+
planets.foreach{
66+
case ("Earth", distance) =>
67+
println(s"Our planet is $distance kilometers from the sun")
68+
case _ =>
7869
}
7970
```
8071

81-
Or, in 'for' comprehension.
72+
Or, in `for` comprehension:
8273

8374
```tut
8475
val numPairs = List((2, 5), (3, -7), (20, 56))
85-
8676
for ((a, b) <- numPairs) {
87-
8877
println(a * b)
89-
9078
}
9179
```
9280

93-
The value () of type Unit is conceptually the same as the value () of type Tuple0. There can only be one value of this type since it has no elements.
81+
## Tuples and case classes
82+
83+
Users may sometimes find it hard to choose between tuples and case classes. Case classes have named elements. The names can improve the readability of some kinds of code. In the planet example above, we might define `case class Planet(name: String, distance: Double)` rather than using tuples.
9484

95-
Users may sometimes find hard to choose between Tuples and case classes. As a rule, case classes are preferred choice if elements carry more meaning.

0 commit comments

Comments
 (0)