lua-users home
lua-l archive

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


Hi,

What would you think of a change in the for protocol like this:

     do
       local f, s, var_1, ···, var_n = explist --keep all init values
       while true do
         local var_1, ···, var_n = f(s, var_1, ···, var_n)  --pass all
values back to f
         if var_1 == nil then break end
         block
       end
     end

Pros:
- allow one to write heapless stateless iterators like values(t) that
can be used in tight loops - I suspect the idiom for _,v in ipairs(t)
is because _ is the state that currently cannot be hidden.

Cons:
- functions that take iterators as arguments and are written like eg.
collect(f,s,v) instead of collect(f,...) will have to adjust but only
to work with new iterators that have more state variables
- there's a subtle semantic change: the hidden variables not
explicitly bound in the loop won't be gc'ed until the next iteration,
just like var_1 and s (can't imagine this breaking any code though).

What do you think? Hope I'm not missing an obvious show-stopper and
spamming for nothing.