Ternary operation

Veterans familiar with C/C++ all know the ternary operation a ? b : c. Using this syntax to replace simple conditional statements can keep the code as concise as possible without making it harder to read.

int a, b, ret;
//if-else
if (a > b)
    ret = a;
else
    ret = b;

//三目运算符
ret = a > b ? a : b;

Ternary operation in Lua

Lua's native semantics do not implement a ternary operation; it is usually simulated using the logical operators and and or.

In Lua, both and and or use "short-circuit evaluation", meaning they only evaluate the second operand when necessary. ("Programming in Lua")

local a, b, ret;
ret = a > b and a or b

Enumerate all possibilities:

When a > b:

a > b and a –> true 
a or b –> a

When a <= b:

a > b and a –> false 
a or b –> b

Perfect!

Pitfalls of the ternary operator in Lua

Following the principle of going from the specific to the general:

  • The general form of the ternary operation a ? b : c

    a = true, result is b
    a = false, result is c

  • Corresponds to a and b or c in Lua

    • b = true
      • a = true
        • a and b –> true
        • b or c –> b
      • a = false
        • a and b –> false
        • b or c –> c
    • b = false
      • a = true
        • a and b –> false
        • b or c –> c
      • a = false
        • a and b –> false
        • b or c –> c

It can be seen that when b = false, the Lua-simulated a and b or c always returns c and cannot restore the original behavior of the ternary operator.

"Programming in Lua" also recommends using if-else to avoid this situation.

Generalized a and b or c

Is there a way to solve the problem where b = false fails?

This reminds me of a common C language exam question: Write a macro definition that returns the smaller of two values.

When I first saw this problem in school, my first reaction was:

#define MIN(A,B) A < B ? A : B

However, this kind of writing cannot return correct results in many nested usage contexts. For example: 2 * MIN(3, 4) expands to 2 * 3 < 4 ? 3 : 4 = 6 < 4 ? 3 : 4, and the result is 4.
A more "engineer" style of writing is:

#define MIN(A,B) ((A) < (B) ? (A) : (B))

Curious readers may refer to: The correct way to write a macro definition, macro definition of the ternary operation

From this example, I started thinking: how can we ensure that b in a and b or c is true, or that b does not cause ambiguity?

  • and has higher operator precedence than or; simply changing the order of operations does not help.
  • Then I thought of the all-purpose table in Lua. Can we put a, b, and c all into tables to change the presence of b? Note that {nil} is also a truthy object.
    Replace a, b, c all with tables: {a} and {b} or {c}.
  • In the ternary operation, a is the condition and the result is b or c. In fact, a does not need to be put into a table; otherwise {a} would always be true, losing the meaning of the condition. And {b} or
    the result of {c} is necessarily a table, and that table contains only one element. Then it can be accessed through
  • In summary, a more generalized Lua ternary operation is: (a and {b} or {c})

Summary

Admittedly, to know the form of the ternary operation in Lua, you can simply search for it and find the desired answer. The above reasoning process is somewhat suspected of already knowing the answer and deliberately leaning toward it. However, I firmly believe that when proving a problem, the conclusion is not the most important thing; the process of argumentation is. This article attempts to reconstruct the derivation of the generalized Lua ternary operation based only on my shallow understanding of Lua. If there is a better reasoning process, we can exchange ideas.

Source: https://blog.csdn.net/u010832643/article/details/77546887