[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: '__iter', yet again!
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Thu, 17 Dec 2009 15:29:26 +0100
2009/12/16 John Hind <john.hind@zen.co.uk>:
> Whereas '__iter' would be taking this historical evolution to its logical
> conclusion!
>
> Generic For undoubtedly was a significant complication of the language, much
> more significant than "__iter" would be.
>
> But I would be interested to understand the reasoning behind implementing
> Generic For as it seems to be subject to the same criticisms you are now
> levelling at "__iter" - the old way was simpler and required no language
> innovations, so why was it deprecated?
>
> I am in two minds between table.foreach and Generic For with __iter, but I
> find Generic For without __iter to be a rather half-assed implementation!
After thinking more about what you want, I realize it would be easy to
implement using token filters. All you need is a filter that converts:
for k,v in <expressions> do end
into:
for k,v in __iter_handler( <expressions> ) do end
With __iter_handler being a function like that:
function __iter_handler(iterator, ...)
local mt = debug.getmetatable(iterator)
if mt and mt.__iter then
return mt.__iter(iterator, ...)
else
return iterator, ...
end
end
Of course the token filter is not trivial because it would have to
recognize the expression end, but that is a feature many token filters
need, so you may be able to find it already implemented.
I've never used token filters myself, but as simple as it is, that
patch fills many customization needs. It would definitely be a good
"official" addition to Lua (with a better API to be able to remove it
from a sandbox though).