1

Closed

Variable set to nil within a function

description

If a local variable inside of a function shares its name with a variable that was passed into the function, the earlier instance of the variable is set to nil. This was found in 0.9 beta.
x=10

testFunction1 = function(y)
   print(y)
   if y == nil then
      print("y equals: nil")
   else
      print("y equals: " .. y)
   end
   local y = 20
   print(y)
end

testFunction2 = function(y)
   print(y)
   if y == nil then
      print("y equals: nil")
   else
      print("y equals: " .. y)
   end
   local z = 20
   print(z)
end
Output:
testFunction1(x)

y equals: nil
20
testFunction2(x)
10
y equals: 10
20
Closed Sep 18, 2014 at 10:15 AM by NeoLithos

comments

wrote Sep 18, 2014 at 10:15 AM

Aufgelöst mit Changeset 31726: Fix: Local variables should not override function parameters

NeoLithos wrote Sep 18, 2014 at 10:19 AM

Thank you,

the parser generated a wrong function body.
function test(x)
  local x = nil;

  print(x);
  x = 20;
  print(x);
end;
Now, it will generate something like that:
function test(x)
  print(x);
  x = 20;
  print(x);
end;
I will release the version within the next days.