lua-users home
lua-l archive

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



There's been a discussion about this earlier, for those not familiar check: http://lua-users.org/lists/lua-l/2007-06/msg00450.html (hope I found the right thread)

There was no clear outcome though, so the issue remains.

In regard to 'select' looking nasty, it can be eliminated using token filters (#..., ...[1] and so on). So I'm not touching that.

The real issue seems to remain, nondeterministic behavor of #tbl in case tbl has holes.

At least this could be done: when crafting a table from ... it would be crafted specially so that #tbl always gives the last nonnil index's position (skips holes). This would need a small flag for each table structure, but would otherwise place the overhead where it's really needed. This must have already been proposed; please see the archives.

The same mechanism could then manually be used to create special holy :) tables which can have holes, without changing the default table behaviour or syntax.

local tbl= table.new( [optional contents ...-like arguments list] )

Would this work?

Another chance would be to have Lua sources compile either hole- ignoring (current, faster) or hole-safe. This would cause problems though, since code would easily start relying on the latter behaviour. In my opinion it would still be worth it (to have # deterministic) since the errors hit hard especially Lua newbies and make the language harder to drive.

-asko


Miles Bader kirjoitti 28.10.2007 kello 3:51:

Miles Bader <miles@gnu.org> writes:
That's exactly my point. That code is the clear and natural way to use
{...}, but it also breaks whenever any of the args in ... is nil.

No it doesn't (I tried it)...

I take this back -- I tried some more test cases, and it _sometimes_
works (with nils), but sometimes doesn't. Specifically, in runs into the
weird behavior in Lua with respect to "#t".

So the main issue is keeping track of the number of arguments, you can't
just use #{...}.

For the case of immediate argument processing, it seems to me that
select is OK -- it's not as pretty as {...}, but it's probably more
efficient.

For the case where you want to bundle up the args and pass them to
another function for processing, I guess you've got to pass in the
number of arguments explicitly, or record them in the "arg table"
({...}) using a non-integer key.

Luckily that sort of thing can be encapsulated in handy functions... :-)
E.g.:

   # handy function
   function argvec(...)
     local args = {...}
     args.num = select ('#', ...)
     return args
   end

Then:

   function bar (...)
      local args = argvec(...)
      for i = 1, args.num do
         print (tostring(i).."\t"..tostring(args[i]))
      end
   end

seems pretty nice  :-)

[FWIW I don't really like the "nil value means delete" behavior of Lua
either; I wish Lua had an explicit table delete operator, and treated
nil as a normal value. So I'd support adding a new table type that had
these properties.  OTOH, I'd also like a bunch of other little changes
(e.g. more flexible behavior to allow natural use of 0-based arrays),
and I guess it's unlikely most of them will ever end up in the official
language ... :-]

-Miles

--
We are all lying in the gutter, but some of us are looking at the stars.
-Oscar Wilde