GooseSerbus
GooseSerbus

Reputation: 1260

Can Lua's require function return multiple results?

It is possible to create a Lua module which returns multiple results via the require function? I'm currently writing an extension to package.loaders and I want to know if I need to support such behavior.

For example, take the following module, named mod.lua:

print("module loading")
return "string1", "string2"

Which is required by the following script:

print("running script")
s1, s2 = require("mod")
print("s1: " .. tostring(s1))
print("s2: " .. tostring(s2))

Results in the following output:

running script
module loading
s1: string1
s2: nil

When I would expect the second string to be returned. I'm not looking to use such behavior, and I realise that you could replicate it by returning a table and unpacking that, I just want to know if it's meant to work (as it's valid Lua syntax) and I can't find a definitive answer on this anywhere.

Upvotes: 22

Views: 7358

Answers (5)

koyaanisqatsi
koyaanisqatsi

Reputation: 2793

When a required Lua script returns multiple results in a table like...

-- req.lua
return {one = 1, two = 2, three = 3}

...then you can catch one of them with...

print(require('req').two) -- Output is: 2

Because only the first return value goes into package.loaded.req.
Therefore multiple results that are not in a table doesnt make much sense for a script thats designed for require().

Upvotes: 0

MonkeyPanic
MonkeyPanic

Reputation: 11

this is how I do it:

foo.lua

return {val1, val2}

bar.lua

myval1, myval2 = unpack(require('foo'))

for you it might be table.unpack, not sure

returning a function is also nifty, but I think upack(require()) tells you exactly what is going on.

Upvotes: 1

m2mm4m
m2mm4m

Reputation: 1

Sometimes it may be preferable to pass-back a 'table' if entries, depending on what you need to return.


-- -- -- -- -- Parent.Lua -- -- -- -- --

local ChildReturns=require("Child");
print("The favourite toy is "..ChildReturns.GetFavourieToy());
print("List ALL toys on the child's favourites list:-");
for F_vK,F_vV in ipairs(ChildReturns) do
   print(F_vK,F_vV);
end

-- -- -- -- -- Parent.Lua -- -- -- -- --

local ChildReturns=require("Child");
print("The favourite toy is "..ChildReturns.GetFavourieToy());
print("List ALL toys on the child's favourites list:-");
for F_vK,F_vV in ipairs(ChildReturns) do
    print(F_vK,F_vV);
end

== == == ==

The favourite toy is Lego
List ALL toys on the child's favourites list:-
1       Lego
2       Meccano

And if needed one could even use the 'unpack' command.

Upvotes: 0

Peter Smith
Peter Smith

Reputation: 753

You could always return a function from your module and have that return multiple values, like below:

foo.lua

return function() return "abc", 123 end

bar.lua

local a, b = require "foo" ()

Upvotes: 16

Andrey Starodubtsev
Andrey Starodubtsev

Reputation: 5292

Lua 5.1.3
require lua export implemented in static int ll_require (lua_State *L) in loadlib.c file. This functions always returns 1 as number of returned values on stack.

Upvotes: 12

Related Questions