Multiple Return Values in Scala
Scala has built-in support for multiple return values through tuples. This feature is used often in idiomatic Scala, for example to return both result and error values from a function.
When you run this program, you’ll see:
In Scala, we use tuples to return multiple values from a function. The function vals()
returns a tuple (Int, Int)
, which is then deconstructed in the main
function using pattern matching.
The underscore _
in Scala serves a similar purpose to Go’s blank identifier, allowing you to ignore certain values when destructuring tuples or in other pattern matching scenarios.
Accepting a variable number of arguments is another nice feature of Scala functions; we’ll look at this in a future example.