lua-users home
lua-l archive

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


On Friday 29 October 2004 13:32, Mike Pall wrote:
[...]
> I suggest to provide both an iterator and a return-all-children-on-stack
> function for convenience.

The problem with iterators is that I have problems if the user starts 
rearranging the hierarchy. The only safe thing to do is to take a snapshot of 
the children and return that.

It does occur to me, however, that I don't need to implement everything in C. 
This would probably work as an iterator:

function childrenof(object)
 local c = {object:getchildren()}
 local n = table.getn(c)
 local i = 1
 return function()
  if (i < n) then
   local o = c[i]
   i = i + 1
   return o
  end
  return nil
 end
end

Actually, this is probably better!

function childrenof(object)
 return ipairs({object:getchildren()})
end

Adding Lua-based functions to a C-based metatable is a bit unpleasant, but 
perfectly possible.

BTW, I've noticed that this works:

for i, j in table do
 print(i, "=", j)
end

It seems to do the same thing as pairs(table). Is this intended?

-- 
+- David Given --McQ-+ "There are two major products that come out of
|  dg@cowlark.com    | Berkeley: LSD and Unix. We don't believe this to be
| (dg@tao-group.com) | a coincidence." --- Jeremy S. Anderson
+- www.cowlark.com --+