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 <steve.j.donovan@gmail.com> wrote:
> On Tue, Mar 1, 2011 at 1:30 PM, Gunnar Zötl <gz@tset.de> 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]
      end
      return T
    end

See:

  http://lua-users.org/lists/lua-l/2011-02/msg01467.html
  http://lua-users.org/lists/lua-l/2011-02/msg01477.html
  http://www.lua.org/work/doc/manual.html#pdf-table.pack
  http://www.lua.org/work/doc/manual.html#lua_gettop
  http://www.lua.org/work/doc/manual.html#lua_settop

Cheers,
  Eduardo Ochs
  eduardoochs@gmail.com
  http://angg.twu.net/