lua-users home
lua-l archive

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


Can anybody explain the following behaviour of
Lua 5.0a? 

Setting 'bug = true' in the first line and running
the code gives the "aargh" error, but setting 
'bug = false' it runs OK. Yet the two branches
should be semantically identical, or have I missed
something?

-- set bug = true/false ------------
bug = true
setglobal = function (var,val)
            local globs = getglobals(2)
            globs[var] = val
            setglobals(2,globs)
            end
names = { "good" }
f = function (x,y)
    assert(x,"aargh")
    return x.." "..y
    end
curry = function (x)
        return function (y)
               return f(x,y)
               end
        end
for _,s in names do
if bug then
  setglobal(s,function(y)
               return f(s,y)
              end)
else
  setglobal(s,curry(s))
end -- if
end -- for
print(good "morning")
----- end --------------------------

With due care about upvalues, Lua 4.0 worked OK both
ways. If you replace the line 'names = { "good" }'
by 's = "good"' and you remove the lines
'for _,s in names do' and 'end -- for', then there
are no problems. 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?

-- 
Gavin Wraith (gavin@wraith.u-net.com)
Home page: http://www.wraith.u-net.com/