Multiple Results and Varargs
Lesson 19 of 48 in Coddy's Luau for Dart Developers course.
Dart records or collection values can package several results in one return value. Luau returns multiple values directly, with expansion controlled by position. A single destination receives only the first result. Use select to count varargs that might include nil rather than relying on table length.
Dart comparison
void main() {
(int,int) pair()=>(2,5); final result=pair(); print(result.$1+result.$2);
}
Luau behavior
Luau functions can return multiple results without constructing a tuple. Annotate a two-result function with a result list such as (number, number). A single assignment keeps only the first result; parentheses around a call can also collapse it to one value.
local function pair(): (number, number) return 2, 5 end
local a, b = pair()
print(a + b)Expected Luau output:
7Challenge
EasyInput is a sequence of integer lines, possibly empty. Define bounds(...) using varargs. Return minimum and maximum separated by a colon, or empty when no values are supplied.
Edit app.luau. The visible locked main.luau reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Try it yourself
local app = require("./app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Luau for Dart Developers
1First Luau Programs
Run Luau ModulesPrint and CommentNames and Local ScopeNumbers and ConversionStrings and Byte IndexesRecap: Format a Receipt4Functions and Closures
Functions and ReturnsDefaults and Options TablesMultiple Results and VarargsCallbacks and Captured StateRecap: Pricing Functions2Values and Decisions
Missing Fields and OptionalsTruth and Default ValuesBranches and BoundsEquality and IdentityRecap: Admission RulesPractice on your own: Online Luau compiler