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

"'Calimero' via lua-l" <[email protected]> Sat, 27 Jun 2026 18:55:58 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
Le 27/06/2026 à 17:37, Federico Ferri a écrit :
> The use of x and y or z is good only for selected trivial cases.
> However nothing prevents you to write a more robust helper:
>
> function _if(cond, _then, _else)
>     if cond then return _then else return _else end
> end
>
> print(_if(10 < 11, 'yes', 'no')) -- yes
> print(_if(10 < 11, false, 0)) -- false
The problem is that this eagerly evaluates boh the _then and the _else.

It costs performance, but more importantly if there are effects, the effects of both branches are executed.


> But, to be honest, I'd like Lua to provide a mechanism to allow me write *better* syntax sugar which would allow me to solve 100 different problems, rather than just one solution to one problem (the ternary operator) which is a nice to have, but it may or may not fit the minimalistic design of Lua.
The conditional is a very fundamental notion in programming, and there is a fundamental flaw in not having an expression conditional.
I'm trying to deal with this.

I do think that it will solve more many problems to have a clean conditional expression.

So… you basically want to allow programs to introduce new syntax? Wouldn't something like user-defined mixfix operators be the minimum necessary to achieve this satisfactorily?

-- 
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/b0a336e1-28dd-e5b7-7773-7cbede18dcf0%40free.fr.