Re: Replacing the "if" statement with an "if" expression which supersedes it
"'Calimero' via lua-l" <[email protected]> Fri, 26 Jun 2026 21:11:46 +0200
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <[email protected]> |
Le 26/06/2026 à 20:08, Andrey Dobrovolsky a écrit :
> Hi Calimero,
>
> I'm glad that You like community proposed snippets so much. I'd like
> to propose a pair of even easier to understand
>
> (select(a and 1 or 2, b, c))
>
> ({[true] = b, [false] = c})[a]
>
> Hope You'll enjoy ))
>
> -- Andrew
I sense sarcasm, but actually there is something to say here.
In the second case, b and c would be both evaluated. And if they have effects, the effects / side effects of both b and c would happen.
This is why `if` has such a special status: it is not eager, it executes only one of the branches.
--
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/23e25cde-201b-ad0f-c741-309f19429151%40free.fr.