lua-users home
lua-l archive

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




On Tue, Jul 23, 2019 at 11:10 AM Viacheslav Usov <via.usov@gmail.com> wrote:
On Tue, Jul 23, 2019 at 4:36 PM Coda Highland <chighland@gmail.com> wrote:

> Python programmers want assignment expressions because Python's comprehension syntax is so heavily used.

This is not a valid reason why a similar device cannot be beneficial in Lua.

Both examples shown in the rationale can be easily adapted to Lua and would be just as convincing.

So I would say the benefit of such a mechanism is not specific to Python and it is valid to consider it for Lua, too.

Just about any procedural language (Lua included) might force the user to write something like:

local x = func() -- expensive
if x > 0 then
  -- use x
end

The problem with the above is the pollution of an outer namespace, not to mention it is forced rather than natural. A bigger problem with that is the scope of the variable thus introduced with respect to its termination (to-be-closing) - instead of being that of the if-statement, it is the outer scope.

Ultimately, Lua has its own problem where one cannot write

local x = foo(), y = bar(x)

The correct version of that is more verbose and feels forced and unnatural. More so when it combines with the previous problem.

So while we are talking about all the weird and wonderful new semantics for Lua variables, it might be prudent to consider this, as well. Perhaps we could end up with something synthesizing multiple benefits, thus making it more likely to appear in Lua.
 
Cheers,
V.

Even the PEP in question says that this should be done with an assignment statement in the if/then example you provide. Assignments instead of comparisons inside of conditionals are one of the biggest reasons NOT to support assignment expressions. Python is working around the problem with it by introducing a new token to the grammar, but it's pretty explicitly stated in the PEP that comprehensions are a major part of the rationale. I presume that being able to use the lambda syntax to express assignments in callbacks is another. (And to be honest, I disagree with the rationale: comprehensions are a very functional-programming kind of thing, and assignments are by definition side-effect-bearing.) And Lua doesn't have comprehensions OR lambdas.

The other problem you cite I consider to be an additional reason NOT to do it. If making good use of something requires expanding the scope of the change, that's a big red flag, especially in a language whose design is as conservative as Lua's.

/s/ Adam