lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Marco Mastropaolo <> wrote:

> Personally I find the "if" statement so much more readable, that I'd
> discard any alternative on that ground alone.
> I agree with Javier - input validation and failfasts at the beginning of
> the function, with if statements.
> Having "return" as an expression would also cause a mess of difficult to
> understand behaviors.. like for example :
> > return nil, Return(0) -- does it return.. what ?
> > f(Return(0)); -- is f ever called ?
> > t[Return(0)] = f(Return(1)) + f(Return(2)) -- .. what ?
> If there's really a need to save keystrokes on such scenarios I'd suggest
> a "on" pattern which just takes a single statement instead of a block
> like:
> > on divisor == 0 return nil, "error"
> > on divisor == 0 do return nil, "error"
> or something like that.
> But then, I don't see such a need :)
> Of course, my 2 cents.

This is actually the approach I took with Lunia (my custom version of Lua)
as I disliked having a single 'return' or 'goto' inside a 'then/end' block.

Therefore in Lunia the following are possible:

if x == 1 goto label;
if x == 1 break;
if x == 1 return true;

Which, to me, looks nicer than this:

if x == 1 then goto label end
if x == 1 then break end
if x == 1 then return true end

Of course, with this shortcut style 'else' and 'elseif' are not possible,
nor does the shortcut-if syntax require an 'end' statement.

Also note that this syntax only allows specific keywords to replace the
'then' keyword after the 'if' expression, not arbitrary statements.

Your suggestion of a new 'on' keyword would allow for single statement
versions of the 'if' statement, however, I found that almost all of my
use-cases for a shortcut-if syntax involved 'goto', 'break' and 'return'
and the rest could just use the traditional 'then/end' syntax.