Outdated Notice
A tuple is a neat class that gives you a simple way to store heterogeneous (different) items in the same container. For example, assuming that you have a class like this:
class Person(var name: String)
Instead of having to create an ad-hoc class to store things in, like this:
class SomeThings(i: Int, s: String, p: Person)
you can just create a tuple like this:
val t = (3, "Three", new Person("Al"))
As shown, just put some elements inside parentheses, and you have a tuple. Scala tuples can contain between two and 22 items, and they’re useful for those times when you just need to combine a few things together, and don’t want the baggage of having to define a class, especially when that class feels a little “artificial” or phony.
Technically, Scala 2.x has classes named
Tuple2
,Tuple3
… up toTuple22
. As a practical matter you rarely need to know this, but it’s also good to know what’s going on under the hood. (And this architecture is being improved in Scala 3.)
A few more tuple details
Here’s a two-element tuple:
scala> val d = ("Maggie", 30)
d: (String, Int) = (Maggie,30)
Notice that it contains two different types. Here’s a three-element tuple:
scala> case class Person(name: String)
defined class Person
scala> val t = (3, "Three", new Person("David"))
t: (Int, java.lang.String, Person) = (3,Three,Person(David))
There are a few ways to access tuple elements. One approach is to access them by element number, where the number is preceded by an underscore:
scala> t._1
res1: Int = 3
scala> t._2
res2: java.lang.String = Three
scala> t._3
res3: Person = Person(David)
Another cool approach is to access them like this:
scala> val(x, y, z) = (3, "Three", new Person("David"))
x: Int = 3
y: String = Three
z: Person = Person(David)
Technically this approach involves a form of pattern-matching, and it’s a great way to assign tuple elements to variables.
Returning a tuple from a method
A place where this is nice is when you want to return multiple values from a method. For example, here’s a method that returns a tuple:
def getStockInfo = {
// other code here ...
("NFLX", 100.00, 101.00) // this is a Tuple3
}
Now you can call that method and assign variable names to the return values:
val (symbol, currentPrice, bidPrice) = getStockInfo
The REPL demonstrates how this works:
scala> val (symbol, currentPrice, bidPrice) = getStockInfo
symbol: String = NFLX
currentPrice: Double = 100.0
bidPrice: Double = 101.0
For cases like this where it feels like overkill to create a class for the method’s return type, a tuple is very convenient.
Tuples aren’t collections
Technically, Scala 2.x tuples aren’t collections classes, they’re just a convenient little container. Because they aren’t a collection, they don’t have methods like map
, filter
, etc.
Contributors to this page:
Contents
- Introduction
- Prelude꞉ A Taste of Scala
- Preliminaries
- Scala Features
- Hello, World
- Hello, World - Version 2
- The Scala REPL
- Two Types of Variables
- The Type is Optional
- A Few Built-In Types
- Two Notes About Strings
- Command-Line I/O
- Control Structures
- The if/then/else Construct
- for Loops
- for Expressions
- match Expressions
- try/catch/finally Expressions
- Scala Classes
- Auxiliary Class Constructors
- Supplying Default Values for Constructor Parameters
- A First Look at Scala Methods
- Enumerations (and a Complete Pizza Class)
- Scala Traits and Abstract Classes
- Using Scala Traits as Interfaces
- Using Scala Traits Like Abstract Classes
- Abstract Classes
- Scala Collections
- The ArrayBuffer Class
- The List Class
- The Vector Class
- The Map Class
- The Set Class
- Anonymous Functions
- Common Sequence Methods
- Common Map Methods
- A Few Miscellaneous Items
- Tuples
- An OOP Example
- sbt and ScalaTest
- The most used scala build tool (sbt)
- Using ScalaTest with sbt
- Writing BDD Style Tests with ScalaTest and sbt
- Functional Programming
- Pure Functions
- Passing Functions Around
- No Null Values
- Companion Objects
- Case Classes
- Case Objects
- Functional Error Handling in Scala
- Concurrency
- Scala Futures
- Where To Go Next