lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> This is not entirely true on at least two accounts:

> 1)  foo will be called with 2 arguments instead of none.
> 2)  the for loop will _not_ stop for x==false, but only for x==nil.

Both true. If foo is a Lua function, of course, it will ignore extra
arguments,
but that is not completely desirable. I don't take any responsibility for
the false/nil thing; I always said it would complicate Lua and it does :)

A simple wrapper solves both problems, sort of the functional equivalent
of pairs:

local function advance(fn)
  return fn() or nil
end

function progs(fn)
  return advance, fn
end

The "fn() or nil" workaround, aside from being a little ugly, prevents a
tailcall optimisation. But c'est la vie.