lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Feb 22, 2006, at 4:02 AM, David Given wrote:
On Wednesday 22 February 2006 05:01, Gavin Kistner wrote:
Is there a clean way to do the above (catch, save, and then later
return) in Lua? (I had hoped I could use a vararg expression
magically in place of "*matches" above, but obviously that doesn't work.

You can do this:

Wow, that's an impressive hack :)

I have no idea about how the internals of Lua work. Could someone who does provide a really rough idea of how hard it would be to extend the vararg syntax to work with:
     local a, b, ... = func(  )

I suppose the hard part about that is that it would only give you a single vararg variable to work with at any time, which you'd need to package/unpackage in order to save:
     local saved_list = { ... }
     local a, b, ... = func( )

     local more = { ... }
     ... = unpack( saved_list )
     -- later
     return unpack( more ), ...

I don't want to turn Lua into Ruby, but Ruby's solution in this area seems reasonably elegant. (Especially now that the length operator in 5.1 has broken the ground for using odd non-standard characters as unary prefix operators.)

I'd love to hear more discussion about adding a 'splat' operator to Lua, so that:

    local a, b, *more = manyValues( )

would be the same as:

    local a, b, v1, v2, v3, ..., vn = manyValues( )
    local more = { v1, v2, v3, ..., vn }

or the same as the syntax I (sort of) propose above:

    local a, b, ... = manyValues( )
    local more = { ... }

And far less important, that the syntax:

    local a, b, c = *more

would be the same as:

    local a, b, c = unpack( more )