[LUA] Local function cannot access itself

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

[LUA] Local function cannot access itself

Post by Ranguna259 »

Code: Select all

local a = function(i)
  i=i+1
  if i == 5 then
    return i
  else
    return a(i)
  end
end

print(a(1))
This code produces the following error:

Code: Select all

input:6: attempt to call a nil value (global 'a')
"a" should access itself because it was declaired before the compiler read it so the scope it's in should be reachable by itself but by the looks of it, this is not how it works so how am I supposed to create function "a" while keeping it local ?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Rucikir
Party member
Posts: 129
Joined: Tue Nov 05, 2013 6:33 pm

Re: [LUA] Local function cannot access itself

Post by Rucikir »

Taking inspiration from kikito's middleclass
Finally, a subtle point regarding recursive private methods. If you need to create a private method that calls himself, you will need to declare the variable first, and then (on the next line) initialize it with the function value. Otherwise the variable will not be available when the function is created

Code: Select all

MyClass3 = class('MyClass3')

local _secretRecursiveMethod -- variable declared here
_secretRecursiveMethod= function(self, n) -- and initialized here
  if(n<=0) then
    print( 'Last recursion')
  else
    print ( 'Recursion level ' .. n )
    _secretRecursiveMethod(self, n-1)
  end
end

MyClass3:recurseOver(n)
  _secretRecursiveMethod(self, n)
end
So you have to declare a before setting the function:

Code: Select all

local a
a = function(i)
  i=i+1
  if i == 5 then
    return i
  else
    return a(i)
  end
end

print(a(1))
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: [LUA] Local function cannot access itself

Post by marco.lizza »

You need to declare the function as

Code: Select all

local function a(i)
  -- code
end
in order for it to work, due to Lua scope mechanism (see http://www.lua.org/pil/6.2.html).
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: [LUA] Local function cannot access itself

Post by Ranguna259 »

Yep, both ways work and here I was experimenting with the debug lib. It's in my blood, I got a tendency to make stuff harder than they seem :P
Anyway, thank you both :3

Code: Select all

local a = function(i)
  local a = {debug.getlocal(2,1)}
  a = a[2]
  i = i+1
  if i == 5 then
    return i
  end
  return a(i)
end

print(a(1))
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: [LUA] Local function cannot access itself

Post by zorg »

So, to sum it up:

Code: Select all

-- The syntax sugar
local function name(arg)
  -- something
end
-- is actually equivalent to:
local name; name = function(arg)
  -- something
end
-- and NOT to:
local name = function(arg)
  -- something
end
-- for the exact reason that the last one can not call itself.
As a sidenote, i also believe that something similar happens when one would want to define fields in a table via the "filling the curly brackets" way, and they can't refer to any "previously defined" key, since technically they don't exist yet; the whole thing will be created at once.

Code: Select all

-- Just to illustrate what i mean:
-- this works:
local t = {a = 1, b = 2}
t.b = t.a
-- these do not:
local t = {a = 1, b = a} -- b is set to nil.
local t = {a = 1, b = t[a]} -- errors because i basically did nil[something], where something in this case was nil as well.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 210 guests