Re: Replacing the "if" statement with an "if" expression which supersedes it

Andrey Dobrovolsky <[email protected]> Fri, 26 Jun 2026 23:42:39 +0300
Newsgroups gmane.comp.lang.lua.general
Message-ID <CAN5FJ7d_wVRR0J9+7JMNEY6qkgFk0eh01Giv1yfuEOVJ0jtYrw@mail.gmail.com>
> 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.

In some cases this may be not a bug but a feature, don't You think so?

Agreed, the second case is not the prefered one also because the c
variable should not be nil. Just slightly weird examples of true
ternary operators using current Lua means. Frankly speaking,
readability and understandability are subjective and may depend on
habits. Extensive usage of proposed if-expressions may appear to be
confusing for someone.

At least there are no obstacles for implementing the true ternary
conditionals just today with the current Lua.

Regards,
-- Andrew

пт, 26 черв. 2026 р. о 22:11 'Calimero' via lua-l <[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.

-- 
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/CAN5FJ7d_wVRR0J9%2B7JMNEY6qkgFk0eh01Giv1yfuEOVJ0jtYrw%40mail.gmail.com.