[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PUZZLE] Appending to a list
- From: Dirk Laurie <dirk.laurie@...>
- Date: Fri, 8 Sep 2017 09:03:52 +0200
2017-09-08 7:51 GMT+02:00 Daurnimator <quae@daurnimator.com>:
>
> 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.
Almost that: I called something like
append(t, myfunc(...))
where myfunc has nice explicit return statements returning one value,
except when none of them has been hit, so that at the end of the function
it returns nothing.
> > 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?
I take that reply to mean: there is no deviation from the manual.
Which is certainly correct in the situation you guessed.
> The main take away is that optional middle arguments are evil.
True, but it's too late now.
What the manual might have spelt out, IMHO, is that a function
with optional middle arguments behaves like a vararg function,
since the number of arguments is not constant.