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 ... |
Get Pragmatic Scala now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.