lua-users home
lua-l archive

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


Left side of assignment in previous example, that is the assumption “What should "x=x-1" mean then?”, has an inherent bug. The variable  "x" is not a number.

Assume you have

local h = {}
local mt = {}
function mt:__call( arg ) return arg end
setmetatable( h, mt )
return h


then

  foo = foo( -1 ) -- reassign foo = -1
  foo = foo 1 -- unexpected symbol
  foo = foo -1 -- attempt to perform arithmetic on local
 foo = foo - 1 -- attempt to perform arithmetic on local

Next call after reassign will fail. Third and fourth statement can be fixed with a sub and add metamethod.

The second statement fails due to the parser, and can't be fixed.

Now rewrite as similar and syntactically valid statements with strings

  foo = foo( 'bar' ) -- reassign foo = 'bar'
  foo = foo 'bar' -- reassign foo = 'bar'
  foo = foo .. 'bar' -- attempt to concatenate local

Next call after reassign will fail. Third statement can be fixed with a concat metamethod.


On Thu, Jun 6, 2019 at 10:25 PM Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:
On Thu, Jun 6, 2019 at 12:14 AM John Erling Blad wrote:
It would be better if
it included all literal values, and I can't see how this could be a
problem, but perhaps there are some other reason why this is
disallowed?



What should "x=x-1" mean then?
x = x(-1)
or
x = x - 1
?