Problems with debug.getinfo()
Posted: Thu Aug 25, 2022 11:31 am
Suppose I have a table of functions indexed by name:
It seems that debug.getinfo will have different results depending on how it is called:
This prevents meaningful output when looping over all functions:
Same thing with getting the function name by concatenating strings:
Is there any way to get at the correct function name in those cases as well?
Code: Select all
get_name = function()
print("Entered function "..debug.getinfo(2).name)
end
a = {
test_a = function(x) get_name() print(x) end,
test_b = function(x) get_name() print(2*x) end,
}
Code: Select all
-- As expected:
> a.test_a(123)
Entered function test_a
123
-- As expected:
> a["test_a"](123)
Entered function test_a
123
-- Unexpected:
> f = "test_a"
> a[f](123)
Entered function ?
123
Code: Select all
> for f_name, f in pairs(a) do a[f_name](123) end
Entered function ?
123
Entered function ?
246
Code: Select all
> x = "a"
> a["test_"..x](123)
Entered function ?
123