lua-users home
lua-l archive

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


On Fri, Aug 1, 2014 at 11:41 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2014-08-01 17:28 GMT+02:00 Jerome Vuarand <jerome.vuarand@gmail.com>:
>
>> table.insert already has an optional middle argument, I don't see any
>> issue there (yeah some people complain about it, but some people
>> complain about virtually anything).
>
> A little ad-hominem argument serves to discredit the opposition, no?
>
> Personally I always use Steve Donovan's idea:
>
>    local append, insert = table.insert, table.insert

I was going to recommend:

local function append (t, v)
  return table.insert(t, nil, v)
end

but apparently you get this error:

$ lua
Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> table.insert({}, nil, 1)
stdin:1: bad argument #2 to 'insert' (number expected, got nil)

That kinda sucks. I guess you could do instead

local function append (t, v)
  return table.insert(t, #t, v)
end

but if you *did* want to use the idiom:

table.insert(t, nil, (expr))

you cannot. Maybe that's something that could be changed in 5.3...

-- 
Patrick Donnelly