Multiple return values from require()

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple return values from require()

Matthias Kluwe
Hi!

Today I had the idea to return multiple object contruction functions
from a module. Suppose I have a file "mod.lua":

   local function f1()
      -- ...
   end

   local function f2()
      -- ...
   end

   return f1, f2

Unfortunately, only one of them is returned by require:

> lua -e 'print( require "mod" )'
function: 0x95a84b0

I had the idea after seeing something similar in LuaMatrix
(http://luamatrix.luaforge.net/) which returns two tables at the end
of matrix.lua. These are assigned by 'local matrix, complex = require
"matrix"' in test_matrix.lua, for example.

Clearly, I am missing something here.

Regards,
Matthias
Reply | Threaded
Open this post in threaded view
|

Re: Multiple return values from require()

Linker
You can return a table instead of a list .

On Sat, Oct 17, 2009 at 19:27, Matthias Kluwe <[hidden email]> wrote:
rtunately, only one of them is returned by require:



--
Regards,
Linker Lin
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Multiple return values from require()

Matthias Kluwe
Hi!

2009/10/17 Linker <[hidden email]>:
> You can return a table instead of a list .

Well, I know ... I can return _one_ table, but matrix.lua does return
two of them, and there has to be a trick (well, not a trick obviously,
but I can't find it in the reference manual).

> On Sat, Oct 17, 2009 at 19:27, Matthias Kluwe <[hidden email]> wrote:
>>
>> rtunately, only one of them is returned by require:

Regards,
Matthias
Reply | Threaded
Open this post in threaded view
|

Re: Multiple return values from require()

Duncan Cross
On Sat, Oct 17, 2009 at 7:01 PM, Matthias Kluwe <[hidden email]> wrote:

> Hi!
>
> 2009/10/17 Linker <[hidden email]>:
>> You can return a table instead of a list .
>
> Well, I know ... I can return _one_ table, but matrix.lua does return
> two of them, and there has to be a trick (well, not a trick obviously,
> but I can't find it in the reference manual).
>
>> On Sat, Oct 17, 2009 at 19:27, Matthias Kluwe <[hidden email]> wrote:
>>>
>>> rtunately, only one of them is returned by require:
>
> Regards,
> Matthias
>

I believe when you require() a module, the single first return value
is what gets stored in the package.loaded table, so the next time
require gets called for the same module you will only ever get that
one value back anyway. You would need to write your own variant of
require() to store the results in a table and unpack them etc. for
what you are talking about to properly work. (Disclaimer, I haven't
actually tried this matrix script, this is just from what I
understand.)

-Duncan
Reply | Threaded
Open this post in threaded view
|

Re: Multiple return values from require()

Peter Cawley
In reply to this post by Matthias Kluwe
On Sat, Oct 17, 2009 at 7:01 PM, Matthias Kluwe <[hidden email]> wrote:
> Hi!
>
> 2009/10/17 Linker <[hidden email]>:
>> You can return a table instead of a list .
>
> Well, I know ... I can return _one_ table, but matrix.lua does return
> two of them, and there has to be a trick (well, not a trick obviously,
> but I can't find it in the reference manual).

Look at http://www.lua.org/source/5.1/loadlib.c.html#ll_require - you
will notice that all execution paths terminate with an error or
"return 1;", hence require() can only return a single value.
Reply | Threaded
Open this post in threaded view
|

Re: Multiple return values from require()

Matthias Kluwe
Hi!

2009/10/17 Peter Cawley <[hidden email]>:

> On Sat, Oct 17, 2009 at 7:01 PM, Matthias Kluwe <[hidden email]> wrote:
>> Hi!
>>
>> 2009/10/17 Linker <[hidden email]>:
>>> You can return a table instead of a list .
>>
>> Well, I know ... I can return _one_ table, but matrix.lua does return
>> two of them, and there has to be a trick (well, not a trick obviously,
>> but I can't find it in the reference manual).
>
> Look at http://www.lua.org/source/5.1/loadlib.c.html#ll_require - you
> will notice that all execution paths terminate with an error or
> "return 1;", hence require() can only return a single value.

Right. I was mislead by the above mentioned script which I assumed to
work. I checked now -- it does not.

Thank you for the clarification.

Regards,
Matthias
Reply | Threaded
Open this post in threaded view
|

Re: Multiple return values from require()

Jerome Vuarand
2009/10/18 Matthias Kluwe <[hidden email]>:

> Hi!
>
> 2009/10/17 Peter Cawley <[hidden email]>:
>> On Sat, Oct 17, 2009 at 7:01 PM, Matthias Kluwe <[hidden email]> wrote:
>>> Hi!
>>>
>>> 2009/10/17 Linker <[hidden email]>:
>>>> You can return a table instead of a list .
>>>
>>> Well, I know ... I can return _one_ table, but matrix.lua does return
>>> two of them, and there has to be a trick (well, not a trick obviously,
>>> but I can't find it in the reference manual).
>>
>> Look at http://www.lua.org/source/5.1/loadlib.c.html#ll_require - you
>> will notice that all execution paths terminate with an error or
>> "return 1;", hence require() can only return a single value.
>
> Right. I was mislead by the above mentioned script which I assumed to
> work. I checked now -- it does not.

If that's an old script, it may have worked in a previous version of
Lua (5.0 had a different 'require' function).