小编chr*_* g.的帖子

lua metatable __lt __le __eq强制返回值的布尔值转换

重载__eq,__lt__le在元表中总是将返回值转换为布尔值.

有没有办法获取实际的返回值?

这将在下面的小lua脚本中用于为参数创建表达式树

用法:

print(_.a + _.b - _.c * _.d + _.a) 
         -> prints "(((a+b)-(c*d))+a)" which is perfectly what I would like to have
Run Code Online (Sandbox Code Playgroud)

但它不起作用,print(_.a == _.b)因为返回值转换为布尔值

ps:稍后应使用处理表达式树的函数替换print

- 从lua脚本中剪辑 -

function binop(op1,op2, event)
    if op1[event] then return op1[event](op1, op2) end
    if op2[event] then return op2[event](op1, op2) end
    return nil
end

function eq(op1, op2)return binop(op1,op2, "eq") end
...
function div(op1, op2)return binop(op1,op2, "div") end

function exprObj(tostr)
    expr =  { eq = …
Run Code Online (Sandbox Code Playgroud)

lua operator-overloading metatable

2
推荐指数
1
解决办法
1032
查看次数

标签 统计

lua ×1

metatable ×1

operator-overloading ×1