如何为表中的函数设置名称
例如,我有一个表有
table.insert( t, 1, function()
print ("rock");
end );
没有办法从这个表中获取函数名称。我知道我可以像键一样存储名称,但是如果我想保留数字索引并且还想知道函数名称怎么办? 有什么办法可以做到吗? 谢谢,提前。
For example, I have a table
table.insert( t, 1, function()
print ("rock");
end );
Is there any way to get function name from this table. I know that I can store name like a key, but what if I want to keep numeric index and also I want to know function name?
Is there any way to do it?
Thanks, on advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您有以下代码:
t
则为{[1] = 5}
。 “5”只是一个数字 - 它没有名称,并且与变量“x”无关;这是一个值。在 Lua 中,函数的处理方式与值完全相同:
x 的值不以任何方式、形状或形式与 x 关联。如果您想手动命名函数,可以通过将函数包装在表中来完成,例如:
That's how you do it!
...除非..
..你违反规则!
当 Lua 开发时,开发人员意识到函数的匿名性质将使生产性错误消息难以生成,甚至不可能生成。
你会看到的最好的事情是:
所以,他们这样做是为了在解析 Lua 代码时,它会记录一些调试信息,以使生活更轻松。为了从 Lua 本身访问此信息,提供了调试库。< br>
使用此库中的函数时要非常小心。
为了达到你想要的效果,你必须使用
调试。 getinfo
函数;一个例子:不幸的是,直接作用于函数的 debug.getinfo 形式没有填充
name
参数 (debug.getinfo(x, "n").name == nil
),上面的版本需要您运行该函数。看来是没希望了!
...除非..
..您真的违反规则。
debug.sethook
函数允许您在某些事件发生时中断正在运行的 Lua 代码,甚至可以在这一切发生时进行更改。这与协程相结合,可以让你做一些有趣的黑客事情。以下是
debug.getfuncname
的实现:用法示例:
此函数不是很可靠 - 有时可以工作,但其他时候则不行。调试库非常敏感,所以这是可以预料的。
请注意,您不应该永远将其用于实际的发布代码!仅用于调试!
仍然可以接受的最极端的情况是在已发布的软件上记录错误,以帮助开发人员解决问题。 重要代码不应依赖于调试库中的函数。
祝你好运!
Say you have this code:
t
would then be{[1] = 5}
. "5" is just a number - it has no name, and isn't associated with the variable "x"; it's a value.In Lua, functions are treated exactly the same way, as values:
The value of x is not associated with x in any way, shape, or form. If you want to manually name a function, you can do it by wrapping the function in a table, for example:
That is how you would do it!
...unless..
..you break the rules!
When Lua was developed, the developers realised that the anonymous nature of functions would make productive error messages difficult to produce, if not impossible.
The best thing you'd see would be:
So, they made it so that when Lua code was parsed, it would record some debug information, to make life easier. To access this information from Lua itself, the debug library is provided.
Be very careful with functions in this library.
To achieve your desired effect, you must use the
debug.getinfo
function; an example:Unfortunately, the form of debug.getinfo that operates directly on a function doesn't fill the
name
argument (debug.getinfo(x, "n").name == nil
) and the version above requires you to run the function.It seems hopeless!
...unless..
..you really break the rules.
The
debug.sethook
function allows you to interrupt running Lua code at certain events, and even change things while it's all happening. This, combined with coroutines, allows you to do some interestingly hacky stuff.Here is an implementation of
debug.getfuncname
:Example usage:
This function isn't very reliable - it works sometimes, and doesn't others. The debug library is very touchy, so it's to be expected.
Note that you should never use this for actual release code! Only for debugging!
The most extreme case that is still acceptable is logging errors on piece of released software, to help the developer fix issues. No vital code should depend on functions from the debug library.
Good luck!
该函数没有任何名称。如果您愿意,可以将其分配给命名变量:
如果您想要将命名函数存储到表中,请预先定义它并使用其名称来存储它:
如果这不是您的意思,请提供更多详细信息;例如您希望如何访问该功能。你这个问题问得有点模糊。
The function hasn't got any name. If you want you can assign it to a named variable:
If what you want is storing a named function to the table, define it beforehand and use its name to store it:
If this is not what you meant, give more details; for example how you would like to access the function. You're question is a bit misty.
问题是 table.insert 将表视为一个序列,仅包含数字键。
如果您希望能够将函数调用为
t.fun()
,则必须将表用作关联数组,从而使用字符串作为键。 (顺便说一句,除了 nil 或 NaN 之外的任何类型都可以作为键)您可能还会注意到函数是通过引用传递的。因此,所有函数实际上都是匿名的,只是作为某个键或变量的值存储。
The thing is table.insert considers the table as a sequence, only with numeric keys.
If you want to be able to call the function as
t.fun()
you'll have to use the table as an associative array and hence use a string as key. (BTW any type except nil or NaN are allowed as key)You might also notice that functions are passed by reference. So all functions are actually anonymous, and are just stored as a value to a certain key or variable.
您可以将名称存储在单独的表中。
要获取该函数,可以使用索引。一旦你有了这个函数,你就可以从 function_names 中得到它的名字:
祝你好运!
You can store the names in a separate table.
To get the function, you can use the index. Once you have the function, you can get its name from function_names:
Good luck!