Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions that return multiple values Lua [duplicate]

Tags:

lua

My question is on functions like pairs(). Functions like this return multiple values like this:

function foo()
     x = bar()
     y = x*2
     return x, y
end

x, y = foo()

Is there a way to get just the second value without bothering with the first? Why would this be preferable to simply returning a table?

It seemed to me like this question probably already existed on this site, but I couldn't find it. Sorry if it's a duplicate.

like image 329
Phineas Greene Avatar asked Jun 08 '26 07:06

Phineas Greene


1 Answers

Using Underscores

It is idiomatic in Lua to discard unwanted values by using an underscore:

_, y = foo()

When you encounter something like x, y = foo() in source code, you expect that both x and y will be used, but the underscore convention communicates the intent that the value assigned to _ is not needed and will not be used.

This is the method that seems most common, and is most used in Programming in Lua. This idiom shows up frequently in loops using iterators. For example, the ipairs iterator function returns both an index and a value, but you may only be interested in the value. The usual idiom for this is:

for _, v in ipairs(t) do
   -- some stuff using v
end

Using select

The select function takes an initial argument which may be a number or the string "#", and an arbitrary number of additional arguments. When the initial argument is a number N, all additional arguments starting from the Nth are returned. Since Lua discards unassigned values, this can be used to select a single argument:

y = select(2, foo())

OP example function returns two values, but for functions that return more than two values:

y, z = select(2, bar())

and

_, y, z = bar()

both assign the second and third return values of bar to y and z, respectively.

The underscore method is less verbose, and more clear in my opinion. Using select also adds the overhead of an additional function call. There are cases in which using select makes more sense, e.g., when you want to programmatically select a return value. Which method you choose is largely a matter of taste.

Wrapping Return Values in a Table

Sometimes it makes sense to wrap multiple return values in a table, but as a general rule this is a bad idea. Wrapping values in a table inside of the function means that a new table must be created, and this takes time; in a loop this can mean significantly slower code.

You might have a case where it helps clarify your code to return a table, e.g., a function stats that returns the mean, median, and mode of some data. In this case you might want to collect those statistics in a table instead of returning them as separate values.

If the values returned from a function make sense as a table, return a table.

Final Thoughts

Use the method that makes your code the most clear. In my opinion the underscore method is the best way to do this unless you have a good reason to use one of the other approaches.

like image 173
ad absurdum Avatar answered Jun 10 '26 15:06

ad absurdum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!