有没有办法确定Lua函数的签名? [英] Is there a way to determine the signature of a Lua function?

查看:646
本文介绍了有没有办法确定Lua函数的签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近, Lee Baldwin 展示了如何编写通用的,可变参数memoize函数。我认为在只需要一个参数的情况下返回一个更简单的函数会更好。这是我的假试图:

 本地函数memoize(f)
本地缓存= {}

if select('#',...)== 1然后
返回函数(x)
如果缓存[x]然后
返回缓存[x]
else
local y = f(x)
cache [x] = y
return y
end
end
else
return function (...)
local al = varg_tostring(...)
if cache [al] then b $ b return cache [al]
else
local y = f (...)
cache [al] = y
return y
end
end
end
end

显然,在这种情况下, select('#',...)失败,无论如何我都不会真正做我想做的事。有什么办法可以告诉 memoize 有多少个参数 f 需要?






如果您确实知道,否是一个很好的答案。使用两个单独的 memoize 函数并不重要。

调试信息并从源代码中确定它,但基本上它是一个不,对不起。


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

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?


"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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆