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 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?)                                

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

  Either it's documented that io.lines() (and a few others) only return a
function, or the manual may need some clarification.

  -spc