Lua: 在变量中存储逻辑运算符?

Lua: Storing a logical operator in a variable?

我无法通过 Google 找到任何相关信息,所以我必须在这里问一下。我想做这样的事情(非常伪代码):

y = first_value

x={op_1 = >, op_2 = <, c = some_value}

if first_value x.op_1 x.c then
...
end

该代码对我说的是,如果 first_value 如果大于 x 的 c 值,则执行某些操作。现在,我知道我可以将 op_1 和 op_2 设置为某个值以区分它们,然后使用单独的 if 语句比较值,但我想尽量减少使用的 if 语句的数量。

我只是想知道这样的事情是否可能,甚至可能以不同的形式出现。提前致谢!

不是这样,运算符是语法中的特定符号。但是,您可以使用函数表示 operation

y = first_value

x={op_1 = function(a,b)return a>b end, op_2 = function(a,b)return a<b end, c = some_value}

if x.op1(first_value, x.c) then
  ...
end