[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Abusing Lua iterators for fun and profit
- From: "Soni L." <fakedme@...>
- Date: Fri, 06 Feb 2015 02:22:12 -0200
On 05/02/15 10:00 PM, Sean Conner wrote:
It was thus said that the Great Soni L. once stated:
It's (in my opinion) cleaner and easier to read. Now my question: can
I rely upon this behavior of io.lines() and string.gmatch() to only
return a single function? I tested it on Lua 5.1, 5.2, 5.3 and LuaJIT,
and in each case, only a function was returned. Am I relying upon an
implementation detail? Should I use the first version, where I pass in
the state and value each time, to be "safe"?
According to the manual:
"Returns an *iterator function*"
I believe it is safe to assume "iterator function" means it returns only
a function that returns the next match or nil at the end. (If it said
just "an iterator", THEN it wouldn't be safe to assume so)
I think Roberto will have to chime in here because of replies from
Yes. this is an implementation detail. please do not rely on it. It just
happens to be that state and 'var_1' are nil
IIRC this caught out a few people/modules when run under alternate
implementations (was it Kahlua?)
Sounds like a non-compliant implementation to me.
This says nothing about the reference manual, tho. (or am I missing
I guess the best solution is not to use lines/gmatch at all, but
read/match (or find). See  for something similar.
Either it's documented that io.lines() (and a few others) only return a
function, or the manual may need some clarification.
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.