lua-users home
lua-l archive

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

Hi List,

Back in June, somewhere in the middle of a thread that threw around
various ideas about the next version of Lua, I made a syntax
suggestion that I'd like to bring up again. I'm planning to get a
patch implemented to see how it feels to use and also to see how much
code change would be required - I made a start on this, but I'm still
not all that familiar with Lua's internals, so it might take a while.

My proposal is that if '...' appears just before a comma (in either
the list of arguments in a function call, a separator between two
array elements in a table constructor, or the right hand side of a
multiple variable assignment) or a semicolon (in the case of a
separator between two array elements only) then it changes the meaning
of that comma or semicolon. Instead of truncating or expanding the
list of values that result from evaluating the expression that
preceded it, the full list is preserved.

Here is an example of how I would expect Lua code to look and act with
such a change:

function multivalues()
    return 1, 2, 3

function printvalues(a,b,c,d,e,f)
    print('"' .. tostring(a) .. ',' .. tostring(b) .. ',' .. tostring(c)
     .. tostring(d) .. ',' .. tostring(e) .. ',' .. tostring(f) .. '"')

-- Function calls
printvalues(multivalues(), 4, 5, 6)    --> "1,4,5,6,nil,nil"
printvalues(multivalues()..., 4, 5, 6) --> "1,2,3,4,5,6"

-- Array constructors
ar = {multivalues(), 4, 5, 6}     --> {1,4,5,6}
ar2 = {multivalues()..., 4, 5, 6} --> {1,2,3,4,5,6}

-- RHS of variable assignments
p,q,r,s,t,u = multivalues(), 4, 5, 6    --> p=1, q=4, r=5, s=6, t=nil, u=nil
p,q,r,s,t,u = multivalues()..., 4, 5, 6 --> p=1, q=2, r=3, s=4, t=5,   u=6

If '...' appears after an expression in any other context, I think it
should be ignored, rather than causing an error. This is mainly so
that people can use it on the last element of a list if they wish -
even though the full expansion would still happen without it.

When I originally suggested this a few people said that it looks odd,
particularly in the case of ... ... - I don't really have any argument
for that, it *is* a bit odd. But hopefully, not too odd to be given
consideration. To me, using ... in this way has a kind of symmetrical
elegance, as it already has a meaning of "accommodate a variable
number of values" - but in this case it would mean, accommodate a
variable number of *output* values, rather than input.

A previous idea with a similar intention was to change the meaning of
the semicolon inside an array constructor. See "Extend table
constructor syntax to allow multiple expansion of multireturn
functions" in the LuaPowerPatches page of the Lua Users' Wiki for a
patch that implements this. I think my proposal has two advantages
over it:
- It does not change the meaning of existing valid code.
- It would work not only for array elements in table constructors, but
also the parameters of a function call and the right-hand side of a
multiple variable assignment.

So, what do people think? Is it something you'd ever use? Is it just
too ugly and weird?