Re: Abusing Lua iterators for fun and profit
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Abusing Lua iterators for fun and profit
- From: "Soni L." <fakedme@...>
- Date: 2015年2月06日 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:
http://www.lua.org/manual/5.3/manual.html#pdf-string.gmatch
"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
Daurnimator:
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.
and Roberto:
I guess the best solution is not to use lines/gmatch at all, but
read/match (or find). See [1] for something similar.
[1] http://www.lua.org/pil/7.1.html
This says nothing about the reference manual, tho. (or am I missing
something?)
Either it's documented that io.lines() (and a few others) only return a
function, or the manual may need some clarification.
-spc
--
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.