How to define Scala methods that return multiple items (tuples)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.5, “How to define Scala methods and functions that returns multiple items (tuples).”

Problem

You want to return multiple values from a Scala function, but don’t want to wrap those values in an artificial, makeshift class.

Solution

Although you can return objects from methods just as in other OOP languages, Scala also lets you return multiple values from a method using tuples. To demonstrate this, first define a method that returns a tuple:

def getStockInfo = {
    // other code here ...
    ("NFLX", 100.00, 101.00) // this is a Tuple3
}

Then call that method and assign variable names to the expected return values, enclosing your variable names in parentheses:

val (symbol, currentPrice, bidPrice) = getStockInfo

Running this example in the REPL demonstrates how this works:

scala> val (symbol, currentPrice, bidPrice) = getStockInfo
symbol: java.lang.String = NFLX
currentPrice: Double = 100.0
bidPrice: Double = 101.0

Discussion

In Java, when it would be convenient to be able to return multiple values from a method, the typical workaround is to return those values in a one-off “wrapper” class. For instance, you might create a temporary wrapper class like this:

// java
public class StockInfo {
    String symbol;
    double currentPrice;
    double bidPrice;
    public StockInfo(String symbol, double currentPrice, double bidPrice) {
        this.symbol = symbol;
        this.currentPrice = currentPrice;
        this.bidPrice = bidPrice;
    }
}

Then you could return an instance of this class from a method, like this:

return new StockInfo("NFLX", 100.00, 101.00);

In Scala you don’t need to create a wrapper like this; you can just return the data as a tuple.

Working with tuples

In the example shown in the Solution, the getStockInfo method returned a tuple with three elements, so it is a Tuple3. Tuples can contain up to 22 variables and are implemented as Tuple1 through Tuple22 classes. As a practical matter, you don’t have to think about those specific classes; just create a new tuple by enclosing elements inside parentheses, as shown.

To demonstrate a Tuple2, if you wanted to return only two elements from a method, just put two elements in the parentheses:

def getStockInfo = ("NFLX", 100.00)
val (symbol, currentPrice) = getStockInfo

If you don’t want to assign variable names when calling the method, you can set a variable equal to the tuple the method returns, and then access the tuple values using the following tuple underscore syntax:

scala> val result = getStockInfo
x: (java.lang.String, Double, Double) = (NFLX,100.0)

scala> result._1
res0: java.lang.String = NFLX

scala> result._2
res1: Double = 100.0

As shown, tuple values can be accessed by position as result._1, result._2, and so on. Though this approach can be useful in some situations, your code will generally be clearer if you assign variable names to the values:

val (symbol, currentPrice) = getStockInfo

See Also

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly:

You can find the Scala Cookbook at these locations:

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.