variable function argument to return

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

variable function argument to return

Philipp Kraus
Hello,

I have got a Lua function which is filled by the C API.

function myFunction( ... )
        local args = {...}

        -- do something with args

        return args ?
end

The args is a table, but I would like to return the arguments in an
equal way in which the args are put to the function. My fuction
myFunction should be used in this way:   a,b,c = myFunction( 1, "test",
true ) or
a,b = myFunction( 1, 2 )

How can I do this on the return line?

Thanks a lot

Phil



Reply | Threaded
Open this post in threaded view
|

Re: variable function argument to return

Choonster TheMage
On 26 September 2014 20:32, Philipp Kraus <[hidden email]> wrote:
Hello,

I have got a Lua function which is filled by the C API.

function myFunction( ... )
        local args = {...}

        -- do something with args

        return args ?
end

The args is a table, but I would like to return the arguments in an equal way in which the args are put to the function. My fuction myFunction should be used in this way:   a,b,c = myFunction( 1, "test", true ) or
a,b = myFunction( 1, 2 )

How can I do this on the return line?

Thanks a lot

Phil
 
If you want to return the original arguments, just use `return ...`. If you modify the args table in some way and want to return that as a series of values, use table.unpack (unpack in 5.1).
Reply | Threaded
Open this post in threaded view
|

Re: variable function argument to return

Philipp Kraus

On 2014-09-26 10:36:32 +0000, Choonster TheMage said:


On 26 September 2014 20:32, Philipp Kraus <[hidden email]> wrote:

Hello,


I have got a Lua function which is filled by the C API.


function myFunction( ... )

        local args = {...}


        -- do something with args


        return args ?

end


The args is a table, but I would like to return the arguments in an equal way in which the args are put to the function. My fuction myFunction should be used in this way:   a,b,c = myFunction( 1, "test", true ) or

a,b = myFunction( 1, 2 )


How can I do this on the return line?


Thanks a lot


Phil

 

If you want to return the original arguments, just use `return ...`. If you modify the args table in some way and want to return that as a series of values, use table.unpack (unpack in 5.1).


Thanks the "unpack" call works well :-)


Phil