lua-users home
lua-l archive

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


On 11 April 2015 at 03:45, Dibyendu Majumdar <mobile@majumdar.org.uk> wrote:
> While working on the core API the realization dawned that up-values
> can subvert static typing of local variables :-(
>
>> function x() local i: integer; return function(j) i = j; end; end
>> f=x()
>> f(5)
>> f('hello')
>

The fix I am implementing introduces new opcodes such as SETUPVALI -
this is like SETUPVAL but converts the value to integer. However, a
particular test in closure.lua fails after this fix:

a = {}
for i=1,10 do
  a[i] = {set = function(x) i=x end, get = function () return i end}
  if i == 3 then break end
end
a[2].set('a')

In Ravi the loop variable i is tagged as of type integer (based on
parsing results) - so in this case assigning 'a' is not allowed as it
fails the conversion to integer. So this code generates an error.

Based on earlier post by Roberto - assume this is okay as assigning
values to loop variables is undefined behaviour.

http://lua-users.org/lists/lua-l/2015-03/msg00024.html

Regards
Dibyendu