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:53 AM, Daurnimator <quae@daurnimator.com> wrote:
> On 1 August 2014 11:47, Patrick Donnelly <batrick@batbytes.com> wrote:
>>
>> local function append (t, v)
>>   return table.insert(t, #t, v)
>> end
>
> In this case, why even use table.insert, just have `t[#t+1] = v`

I think the point is that if t is an expression and not just a
variable, then it is annoying/expensive to have it appear twice for a
simple append. e.g.

foo.bar[#foo.bar+1] = rab

> You could also just use
>
>     local function append(t,v)
>         return table.insert(t, v)
>     end
>
> ==> now you'll never accidently pass 3 arguments to insert.

Right, you could just do that.

Of course the idiom could be:

table.insert(t, (expr))

which ensures only 2 arguments. However, I think I like:

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

better. I think it's clearer to the reader of the code what you're
doing and it doesn't matter if expr evaluates to multiple values.

-- 
Patrick Donnelly