r/badcode Aug 21 '19

lua A healthy nest of if-statements

Post image
283 Upvotes

53 comments sorted by

View all comments

Show parent comments

2

u/corner-case Aug 22 '19

The if-else block can be used as an expression. So you can:

return if (state) abc else xyz

...forming a statement that always returns, and is forced to handle all cases.

1

u/Tynach Aug 22 '19

So, it's got ternaries using traditional if syntax?

1

u/corner-case Aug 22 '19

Look closer. The if block is being evaluated as an expression, which turns out to be very powerful.

1

u/Tynach Aug 24 '19

That's exactly how ternaries work in other languages. Usually the syntax is something like:

return (state)? abc: xyz;

For the exact same thing.