lua-users home
lua-l archive

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

On Tue, Mar 1, 2011 at 8:39 AM, steve donovan <> wrote:
> On Tue, Mar 1, 2011 at 1:30 PM, Gunnar Zötl <> wrote:
>> Nope, it should not. Passing three nils is not the same as passing nothing at all.
> And this is in fact useful.
> Consider the library function io.lines(); called without any
> arguments, it iterates over standard input. Pass it a file name, it
> iterates over that file if it can.
> It would _not_ be nice if io.lines(nil) would open stdin, since an
> accidental nil variable is all too common. Instead, it gives you an
> error, as it should.
> steve d.

As far as I can remember (corrections, please?) "nil" only corresponds
"nothing" in these two situations: when vlists are extended (and
internally it is lua_settop that does that) and when table fields are
set... so, "foo(nil, nil, nil)" receives a vlist with dracula 3, while
"foo()" receives a vlist with dracula 0. When packed as tables these
vlists look like {n=3} and {n=0}.

Here's a version of table.insert that accepts an arbitrary number of
objects to insert:

  mytableinsert = function (T, ...)
      local objects = table.pack(...)
      for i=1,n do
        T.n = T.n + 1
        T[n] = objects[i]
      return T


  Eduardo Ochs