[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PUZZLE] Appending to a list
- From: Daurnimator <quae@...>
- Date: Fri, 8 Sep 2017 15:51:33 +1000
On 8 September 2017 at 15:31, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> I used to have the following statement in nearly all of my programs.
>
> local append = tinsert
>
> where tinsert is a local variable with value table.insert. The idea is
> that when the `pos` parameter is omitted, I prefer for the sake of
> easy reading to use a different name.
>
> Today, I needed to do this instead:
>
> local append = function(tbl,x) tinsert(tbl,x) end
>
> Why would that be?
You probably called something like:
append(t, s:gsub("foo", "bar"))
==> gsub returns two values, and the extra argument makes table.insert
do something that's not append.
> Once you have figured it out, the next question is: should I have
> expected table.insert to behave that way or is it a deviation from
> what the manual says?
What is the deviation from the manual?
The main take away is that optional middle arguments are evil.
See http://lua-users.org/lists/lua-l/2014-08/msg00061.html
We can find your reply here:
http://lua-users.org/lists/lua-l/2014-08/msg00063.html