lua-users home
lua-l archive

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


Other than a name-to-function mapping table (like in your first example), I don't think there's a way to do this directly. You could use loadstring() to invoke a function by name, it has to be visible form the compiled function's context, which won't have access to your locals. It would also be inefficient, if you have to do this a lot.

On Mon, Jun 8, 2015 at 11:56 AM, Dmitry V. Zaitsev <hhrhhr@gmail.com> wrote:
example with table of functions:

local T = {}
function T.func1() print("func1") end
function T.func2() print("func2") end

local fn = { "func1", "func2", "func3" }

for i = 1, 3 do
local func_name = fn[i]
pcall(T[func_name])
end

it is work, but I'm interested in the ability to call a local function:

local function func1() print("local func1") end
local function func2() print("local func2") end

local fn = { "func1", "func2", "func3" }

for i = 1, 3 do
local func_name = fn[i]
pcall(func_name) -- ???
end

of course, I get an error "attempt to call a string value".



--
Brigham Toskin