大家都知道的三目运算符在lua中的写法是:
a and b or c
--避免当b是nil的时候会返回c的值的写法:
(a and {b} or {c})[1]
示例:
test = 5
print('test > 0 = ' ..( test > 0 and '成功' or '失败'))
test > 0 = 成功
--或
test = -1
print('test > 0= '..( test > 0 and {'成功'} or {'失败'})[1] )
test > 0 = 失败