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

"'Calimero' via lua-l" <[email protected]> Sat, 27 Jun 2026 15:35:33 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
Le 27/06/2026 à 08:15, Родион Горковенко a écrit :
> Calimero, Hi!
>
> Thanks for clarifications. I also have seen your discussion with Andrew about readability etc.
>
> However I probably failed to clarify my question of internal statements and why I think your proposed change is really immense, not a syntactic sugar.
>
> Your suggested feature allows us to create new variables inside the expression, i.e. expression will affect outer scope.
>
> a = if x > 1 then b = 13 end
>
> Unless I had forgotten something or we use dirty tricks on globals table , expressions are restricted from such behavior.
>
> So you actually propose to create completely new semantics of expression/statements (or something in-between).
>
> This is significant change from the viewpoint both of the source code and of the language paradigm. Do we really need this?
>
> Perhaps we may want something different for ternary operator?
>
> What you propose, blocks with value calculated by the last statement, surely exists in other languages but I think generally with languages which use isolated scope for blocks.
>
> sincerely yours,
> Rodion

Ooh, interesting remark, that!
I suddenly thought I hadn't understood the language well enough, so I investigated this.

A relevant remark is that a local variable declared in a loop, *as well as in an if statement*, does not exist any more outside of that loop or if statement.


For the sake of assurance, I ran this:

for i = 1,3 do local a = "a local variable leaked out of a loop" end
print(a)

if true then local b="a local variable leaked out of an if statement" end
print(b)

function f1() local c="a local variable leaked out of a routine" end
_ = f1()
print(c)

for i = 1,3 do d = "a global variable leaked out of a loop" end
print(d)

if true then e="a local variable leaked out of an if statement" end
print(e)

function f2() f="a global variable leaked out of a routine" end
_ = f2()
print(f)

Of course globals "leak" everywhere all the time.

The above code makes the point that it's entirely possible to be calling a routine in an expression and define a global there too.


I conclude that while your remark was very relevant, in the end it entails no concerns, as it is possible to keep the behaviour of the if statement when turning it into an if expression.
Indeed, the current if statement erases locals declared in one of its branches!

This makes sense, because having variables that could be declared or not depending on the branch taken would cause headaches. My thanks to the designers of Lua for this.


Thanks again for your remark, this was worth double-checking. All good, apparently. In my eyes this consolidates that my suggestion has good compatibility with Lua as it exists today.

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/b034ac5d-25d2-3161-f0fd-8f53ee0b8a1b%40free.fr.