有没有办法确定 Lua 函数的签名? [英] Is there a way to determine the signature of a Lua function?
问题描述
最近,Lee Baldwin 展示了如何编写 通用可变参数记忆函数.我认为返回一个只需要一个参数的更简单的函数会更好.这是我完全虚假的尝试:
Recently, Lee Baldwin showed how to write a generic, variable argument memoize function. I thought it would be better to return a simpler function where only one parameter is required. Here is my total bogus attempt:
local function memoize(f)
local cache = {}
if select('#', ...) == 1 then
return function (x)
if cache[x] then
return cache[x]
else
local y = f(x)
cache[x] = y
return y
end
end
else
return function (...)
local al = varg_tostring(...)
if cache[al] then
return cache[al]
else
local y = f(...)
cache[al] = y
return y
end
end
end
end
显然,select('#', ...)
在这种情况下失败了,无论如何都不会真正做我想做的事.有什么办法可以在memoize里面告诉f期望多少个参数?
Obviously, select('#', ...)
fails in this context and wouldn't really do what I want anyway. Is there any way to tell inside memoize how many arguments f expects?
如果您确定,否"是一个很好的答案.使用两个独立的 memoize 功能没什么大不了的.
"No" is a fine answer if you know for sure. It's not a big deal to use two separate memoize functions.
推荐答案
我想你可以进入调试信息并从源代码中确定这一点,但基本上它是不",抱歉.
I guess you could go into the debug info and determine this from the source-code, but basically it's a "no", sorry.
这篇关于有没有办法确定 Lua 函数的签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!