lua-users home
lua-l archive

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


It was thus said that the Great Tim Hill once stated:
> 
> > On Dec 10, 2014, at 9:32 AM, Sean Conner <sean@conman.org> wrote:
> > 
> > If that were true, then ipairs() would be removed as you can always do:
> > 
> > 	for i = 1 , #t do ... end
> 
> As has been noted before, this does not have the same behavior as ipairs()
> if the table is not a valid sequence. (Earlier betas of 5.3 did have this
> behavior, but it has been reverted to stop the iteration at the first nil
> value, which is not necessarily at #t+1 for non-sequences.)

  Hmm ... I just tried the following in Lua 5.2 and it worked:

	mt = 
	{
	  __len = function(s) return 100 end,
	  __index = function(s,k) return nil end,    
	}

	x = setmetatable({},mt)

	for i = 1 , #x do
	  print(i,x[i])
	end

  But yes, if that metatable isn't there, then the loop will print nothing.

  -spc