lua-users home
lua-l archive

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

On 23/07/2019 16.10, Pedro Tammela wrote:
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?


function w, h )
  local self = { }
  for y = 1, h do
    local row = self[y] := { }
    for x = 1, w do
      row[x] = { 0, 0, 0 }
  return setmetatable( self, Image )

makes good use of := in

    local row = self[y] := { }

vs. what I usually do

    local row = { } ; self[y] = row

or (worse)

    local row = { }
    for … do
    self[y] = row  -- easy to forget this line

Same for all sorts of other situations where you have to do multi-level

I actually considered suggesting this on the list *before* it came up in
Python, but never actually bothered to because while it is slightly more
readable IMHO, there's just too many questions that would have to be
answered and I don't feel that's worth it.

(What about multiple-return expressions:  Can you assign only the first
value or all of them?  If only the first, do the return values truncate
to one result or do they still all go through?  Is this a pure
side-effect (assign x, result is x) or a store-read (assign x, read x
(which may have been changed by __newindex/__index))? Etc. etc. …)

-- nobody