1

I can't find anything about this through Google so I have to ask here. I want to do something like this (very pseudo code):

y = first_value

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

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

What that code says to me is that if first_value if greater than x's c value then do something. Now, I know I could set op_1 and op_2 to some value to differentiate between them and then compare values using separate if statements, but I would like to minimize the number of if statements used.

I was just wondering if something like this is possible, maybe even in a different form. Thanks in advance!

1
  • That is not possible. You cannot define your own infix operators in Lua. Commented May 11, 2018 at 22:47

1 Answer 1

7

Not this way, an operator is a specific symbol that is part of the syntax. However, you can represent an operation using a function:

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
1
  • Ah, that makes a lot of sense! I was just overcomplicating it. Thanks!
    – JustHeavy
    Commented May 13, 2018 at 17:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.