lua-users home
lua-l archive

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


> So the lack of referential transparency must come down to the use of
> 'setglobal' inside the for-loop. Is this a bug or a feature?

There is a bug in Lua and in the manual :-( But, after all corrected,
your program should work as it is working now ;-)

The bug in the manual is the equivalence code for the "for" loop:

       do
         local _f, _s, var_1 = explist
         while 1 do
           local var_2, ..., var_n
           var_1, ..., var_n = _f(_s, var_1)
           if var_1 == nil then break end
           block
         end
       end

As it is now, the first variable (var_1) has a scope, and the others
(var_2, etc.) have a different scope. This is weird. The "correct"
behavior is for all variables declared together to have the same scope.
Because var_1 must be declared outside the loop (to keep its value
across the loop), all of them should follow; that is

       do
         local _f, _s, var_1, var_2, ..., var_n
         _f, _s, var_1 = explist
         while 1 do
           var_1, ..., var_n = _f(_s, var_1)
           if var_1 == nil then break end
           block
         end
       end

With this correction in the manual, it is easy to see that your "s"
(var_2) is changed to nil before the end of the loop, and this is
its value when "good" is called. 

(The bug in Lua is that the current implementation does not follow
exactly neither the first nor the second model. We must check
that carefully.)

-- Roberto