Re: Replacing the "if" statement with an "if" expression which supersedes it
Francisco Olarte <[email protected]> Sat, 27 Jun 2026 19:02:44 +0200
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <CA+bJJbztnBpXJqWJ8U89CXk8CCvrW5fauzZpAZkYEk0LeFezRg@mail.gmail.com> |
On Sat, 27 Jun 2026 at 08:16, Родион Горковенко <[email protected]> wrote: ... > 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. The original extension was not specified nearly enough for me, and further clarificacions were not of much help, as they began to mix if-expressions with "(nearly) every statement is an expression", what allows C to do a=b=1. If you tackle it by parts, the if-expression, lets call it IF for now to avoid collisions in the parser, is not much than adding "| IFexp" to the or chain in exp and defining "IFexp ::= IF exp THEN exp { ELSEIF exp THEN exp } [ ELSE exp ] end" in "The complete syntax of lua". In this case, as with the classic and-or, assignments will be illegal, as they now are inside exps ( anyway, if yu were caring about the globals table, they are already possible, "a=(x>1) and rawset(_ENV,"b",13)", and cookedset is a one liner if you like to play with metatables in globals. OTOH making assignments an expression opens a real can of worms. This would seem to need a deep change. I like my ternaries, and would love to have them in Lua. As I would love to have lot of other operators, like op-assign of every variety, which I think would improve readabilty in some of the most unreadable places of my code, as well as null coalescing or those fancy elvis operators, but I fear powers that be want to keep Lua simple. Francisco Olarte. -- 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/CA%2BbJJbztnBpXJqWJ8U89CXk8CCvrW5fauzZpAZkYEk0LeFezRg%40mail.gmail.com.