Op Tue, 23 Jul 2019 16:10:09 +0200 schreef Pedro Tammela
<pctammela@gmail.com>:
> In Python 3.8 there will be a new feature introducing Assignment
> Expressions[1]. One of the reasons for such feature is to improve code
> readability, among other things.
>
> Would assignment expressions improve Lua code readability?
>
> [1] https://www.python.org/dev/peps/pep-0572/
Maybe I missed the momentum of the discussion but I would like to see
something similar in Lua.
It encourages to use RAII, and reduces the scope of the variable.
We already have in Lua some form of assignment _expression_ in... for loops.
No one has been complaining about that.
Another example is C++17 that allows you to do something like:
if( auto foo = get_foo() ; foo.bar() == 42 )
{
foo.baz( 1337 );
}
-- Jasper
Lua's for loops don't have assignment expressions. It's actually rather noteworthy compared to other programming languages -- neither the initialization or the increment is a statement.
The C++17 example also looks like something I would NEVER want to write. There's no compelling reason to put that on one line. Just kick the initializer out to the previous line, and add braces around it if you REALLY need the strict scoping.
/s/ Adam