Jus*_*avy 1 lua if-statement operators logical-operators
我无法通过Google找到任何相关信息,因此我必须在此处提问.我想做这样的事情(非常伪代码):
y = first_value
x={op_1 = >, op_2 = <, c = some_value}
if first_value x.op_1 x.c then
...
end
Run Code Online (Sandbox Code Playgroud)
该代码对我说的是,如果first_value大于x的c值,那么就做一些事情.现在,我知道我可以将op_1和op_2设置为某个值来区分它们,然后使用单独的if语句比较值,但我想最小化if语句的使用数量.
我只是想知道这样的事情是否可能,甚至可能是以不同的形式.提前致谢!
不是这样,运算符是特定符号,它是语法的一部分.但是,您可以使用函数表示操作:
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
Run Code Online (Sandbox Code Playgroud)