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?

[1] https://www.python.org/dev/peps/pep-0572/

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

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
    end
    self[y] = row  -- easy to forget this line

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

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