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

Federico Ferri <[email protected]> Sat, 27 Jun 2026 17:37:31 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <CA+VucGDWgB6S_sbccei+ma1frKOwTfG5uBKcm5aBsB0w5vKr6A@mail.gmail.com>
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

And you can add as much syntactic sugar as your use-case needs:

function _if(cond, value)
    return setmetatable({
        _ = {{{cond, value}}, nil},
        _elseif = function(self, cond, value) table.insert(self._[1],
{cond, value}) return self end,
        _else = function(self, value) self._[2] = value return self end,
    }, {
        __call = function(self)
            for _, entry in ipairs(self._[1]) do
                local cond, value = table.unpack(entry)
                if cond then return value end
            end
            return self._[2]
        end,
    })
end

print(_if(10 < 11, 'yes'):_elseif(true, 'no'):_else(8)()) -- yes
print(_if(10 > 11, 'yes'):_elseif(true, false):_else(8)()) -- false
print(_if(10 > 11, 'yes'):_elseif(false, 'no'):_else(8)()) -- 8


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.

On Fri, 26 Jun 2026 at 02:41, 'Calimero' via lua-l <[email protected]>
wrote:

> Hello!
>
> I suggest outright replacing Lua's existing "if statement" with an "if
> expression".
> The expression variant would completely supersede the current statement
> variant, keeping exactly the same syntax.
> This does not make the language bigger, but it does make it more powerful.
>
> It would be a fully backwards-compatible change, with the value of the
> expression being simply ignored in existing codebases.
>
> Details:
> - Lack of an "else" branch would simply mean the value to be "nil" if no
> condition is true (in if or elseif branches).
> - This drags along with it the idea that a block of statements evaluates
> to the value of its latest statement (possibly nil, if no value).
>
>
> Naturally, I have read http://lua-users.org/wiki/TernaryOperator before
> posting this.
>
> I am interested in knowing whether this will be implemented, or failing
> that, why it won't be!
>
>
> --
> 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/5f7ef457-8e06-e0a9-9583-144a6dbea73b%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/CA%2BVucGDWgB6S_sbccei%2Bma1frKOwTfG5uBKcm5aBsB0w5vKr6A%40mail.gmail.com.