A lot of the use cases where you would have used multiple return values in the past simply aren't necessary anymore with modern language features. Want to return an error code? Throw an exception or return an Either<T, Throwable>
. Want to return an optional result? Return an Option<T>
. Want to return one of several types? Return an Either<T1, T2>
or a tagged union.
And even in the cases where you genuinely need to return multiple values, modern languages usually support tuples or some kind of data structure (list, array, dictionary) or objects as well as some form of destructuring bind or pattern matching, which makes packaging up your multiple values into a single value and then destructuring it again into multiple values trivial.
Here are a few examples of languages that do not support returning multiple values. I don't really see how adding support for multiple return values would make them significantly more expressive to offset the cost of a new language feature.
Ruby
def foo; return 1, 2, 3 end
one, two, three = foo
one
# => 1
three
# => 3
Python
def foo(): return 1, 2, 3
one, two, three = foo()
one
# >>> 1
three
# >>> 3
Scala
def foo = (1, 2, 3)
val (one, two, three) = foo
// => one: Int = 1
// => two: Int = 2
// => three: Int = 3
Haskell
let foo = (1, 2, 3)
let (one, two, three) = foo
one
-- > 1
three
-- > 3
Perl6
sub foo { 1, 2, 3 }
my ($one, $two, $three) = foo
$one
# > 1
$three
# > 3
def f(): return (1,2,3)
and then you can use tuple-unpacking to "split" the tuple:a,b,c = f() #a=1,b=2,c=3
. No need to create an array and manually extract elements, no need to define any new class. – Bakuriu Jul 3 at 7:40[a, b] = f()
vs.[a, b, c] = f()
) and obtained insidef
bynargout
. I'm not a big fan of Matlab but this actually comes in quite handy at times. – gerrit Jul 3 at 10:06