Search code examples
lua

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.


Solution

  • 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.