lua-users home
lua-l archive

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




On 2018-01-02 03:21 PM, Michael Nelson wrote:
On 1/2/2018 6:18 AM, Roberto Ierusalimschy wrote:  ...
As already explained, you can use _ARG; but you can use also any other
name:

   function foo (...=a) print(a.n) end

_ARG is present both for compatibility (the above syntax will not even
compile in older versions) and for the main chunk (which does not have
a header to name this parameter).

 I just love the new syntax. I assume select will continue to work as usual, for old code and for use cases where it works better. The _ARG name makes the parameter name for the main chunk consistent with Lua _NAME practice, a good change. Any code broken by this last change should be an easy fix: a simple search and replace.


Hmm... Would the following work (and, if so, would it be faster than the C API/built-in table.unpack)?

table.unpack = function(t, i, j)
  assert(type(t) == "table", "first arg to table.unpack must be table")
  i = i or 1
  j = j or rawlen(t)
  local oldn = rawget(t, "n")
  rawset(t, "n", j)
  local _ARG = t
  local function helper(t, oldn, ...)
    rawset(t, "n", oldn)
    return ...
  end
  return helper(t, oldn, select(i, ...)) -- I guess this select() is the limiting factor.
end

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.