O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Tuples and Multiple Assignments

In Java, while methods can take multiple arguments, they can only return one result. Returning more than a single value needs clumsy workarounds in Java. For example, to return a person’s first name, last name, and email address we’d have to employ a Person class and return an array of Strings or an ArrayList. Scala’s Tuple, combined with multiple assignments, makes returning multiple values a simple task.

A tuple is an immutable object sequence created as comma-separated values. For example, the following represents a tuple with three objects: ("Venkat", "Subramaniam", "venkats@agiledeveloper.com").

You can assign the elements of a tuple into multiple vals or vars in parallel, as we see here:

FromJavaToScala/MultipleAssignment.scala ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required