lua-users home
lua-l archive

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



On Sep 18, 2013, at 5:18 AM, Jayanth Acharya <jayachar88@gmail.com> wrote:

Can someone kindly explain the reason why I see this error ?

[code]
tax = {
 _income = 0,
 _rate = 0,
 _tax = 0
}

tax.init = function (pIncome, pRate)
  tax._income = pIncome
  tax._rate = pRate
  end


tax:init(12200, 12)
print(tax:getIncome(), tax:getRate())
print(tax:getTax())
[/code]

[error]
>lua -e "io.stdout:setvbuf 'no'" "13.lua"
table: 0028CA00    12200
lua: 13.lua:30: attempt to perform arithmetic on field '_income' (a table value)
stack traceback:
    13.lua:30: in function 'getTax'
    13.lua:36: in main chunk
    [C]: ?
>Exit code: 1
[/error]


When you use ":" to call a function, Lua adds the table as the first argument to the function. So:

tax:init(12200, 12)

Actually calls init() as follows: init(tax, 12200, 12)

And of course that stores the 'tax" table in _income, and you are on your way to trying to do math on a table, hence the error.

--Tim