我真的需要在Lua中有一个整数类型.
我所说的整数类型是一个定义通常运算符(/*+ etc)的类型,其行为类似于整数,内部表示无关紧要.
用表做这样的事情非常简单,问题是,我试过,而且性能非常差(当然).这是我的部分实现:
function num_op(a, b, calc_func)
local atype = pytype(a)
local btype = pytype(b)
local a_val, b_val
a_val = (atype == "Integer" or atype == "Double") and a[1] or a
b_val = (btype == "Integer" or btype == "Double") and b[1] or b
val = calc_func(a_val, b_val)
if atype == "Integer" and btype == "Integer" then
return Integer:create(val)
else
return Double:create(val)
end
end
numeric_mt = {
__add = function(a, b)
return num_op(a, b, function(a,b) return a + b …Run Code Online (Sandbox Code Playgroud)