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 Andrew Starks once stated:
> On Wed, Dec 10, 2014 at 11:32 AM, Sean Conner <sean@conman.org> wrote:
> >   I know, there's signalling intent.  But the default iterator from ipairs()
> > supports __index, and in Lua, a sequence for ipairs() is defined as a
> > sequence of integer keys from 1 to n with non-nil values.  [1]
> 
> Hmm. I see your (the author's presumably, as well) point. The same
> effect may be had, except that there is no longer a way to continue
> after the value is nil, except by using some other mechanism, such as
> a while loop.

  Only when the first value returned (which is used as the next key) is nil
does the loop end; other values may be nil.  Here, have an "array" with a
thousand nil values:

	mt = 
	{
	  __ipairs = function(t)
	    local function iter(s,k)
	      if k == 1000 then
	        return nil
	      else
	        return k + 1,nil
	      end
	    end
	    
	    return iter,t,0
	  end
	}
	
	x = setmetatable({},mt)
	
	for index,value in ipairs(x) do
	  print(index,value)
	end

> > [1]     I would really expect Thiago, Rena and Coroutines [2] to be upset
> >         over the removal of __ipairs() as (to me) they seem like the type of
> >         people that love overriding how the language works.
> 
> Perhaps you may be conflating monkey patches and metamethods?

  Perhaps.

> > [2]     What ever happened to Coroutines anyway?
> >
> 
> * I don't pretend to know, but I recognize the pattern in my own
> behavior. Engage. Obsess. Go too far. Retreat. Repeat.

  But he's been gone for a few months now ... 
  
  -spc