lua-users home
lua-l archive

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


Correction:
the code should be:

function (...)

  return unpack{..., "a"}
end

Sorry for the inconvenience.


On Tue, Nov 11, 2008 at 7:14 PM, Jun Wu <idearet@gmail.com> wrote:
It works.
And I suggest t be a local variable.
Since unpack() do the trick, the code can be shorten to:

function (...)
  return unpack(..., "a")
end

Hope it helps.



On Tue, Nov 11, 2008 at 6:26 PM, Linker <linker.m.lin@gmail.com> wrote:
I think you can try this:
function(...)
  t={...}
  t[#t+1]="a"
  return unpack(t)
end



On Tue, Nov 11, 2008 at 17:44, Duncan Cross <duncan.cross@gmail.com> wrote:
On Tue, Nov 11, 2008 at 9:15 AM, Atry <pop.atry@gmail.com> wrote:
I tried the follow code, but it seems not work.

function a(...) return ..., "a" end
assert(select(3, a(1, 2)) == "a");

Is there any way to append a argument to vararg without `{...}' and `unpack' ?

It looks like this is handled in the same way as "return func_call(), a" where func_call returns multiple values. I.e., an _expression_ that evaluates to a list is truncated to the first item when it is itself used as an item in a list (except the last). So, no, I don't think there is a way. Perhaps the Lua team could be convinced that '...' should be treated as a special case in this situation. (I'm not convinced myself, though, to be fair. It seems like there should either be a way to specify that a list of values should be expanded rather than truncated - I'm not sure what the syntax would be - or keep it consistent.)

-Duncan



--
Regards,
Linker M Lin
linker.m.lin@gmail.com