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. For my money, the underscore method is the default way to do this. If you have a good reason to use one of the other approaches, follow your bliss.