Re: Replacing the "if" statement with an "if" expression which supersedes it
"'Calimero' via lua-l" <[email protected]> Fri, 26 Jun 2026 17:44:23 +0200
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <[email protected]> |
Le 26/06/2026 à 17:32, Andrey Dobrovolsky a écrit :
> Greetings!
>
>> http://lua-users.org/wiki/TernaryOperator says :
>>> The main caveat is that if |a| or |c| evaluates to true while |b| or |d| respectively evaluate to false, then this expression >> will not behave exactly like the ternary operator.
> In my understanding it is not a "caveat" (opinionated) but lack of parentheses.
>
> Regards,
> -- Andrew
Well, not quite!
The caveat is still present with extra parentheses, that I can tell:
> (10 < 11) and ("yes") or ("no")
yes
> (10 < 11) and (false) or (0)
0
Can you fix the second example to return `false` as it should, by adding some parentheses? I experimented but failed.
To digress and be entirely honest, the other (and main) caveat of `condition and a or b` is that readability suffers, because objectively it is not stating its intention!
And when it's nested, I feel a bit lost: `x = a and b or c and d or e`
Cheers
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/c2d70b8b-5b43-0e2b-2130-868782ccff30%40free.fr.