GreenSaber
GreenSaber

Reputation: 1148

Lua function getting different datatype when given a number

I have this problem in a Lua class. Here is the code of my class:

local Temp = {}             

function Temp:new(tmp)             
  local self = {temp = -273.15}    
  if tmp > self.temp then       
    self.temp = tmp
  end

  local setC = function(usrTmp)
    if usrTmp < -273.15 then
      self.temp = -273.15
    else
      self.temp = usrTmp
    end
  end

  local getC = function()         
    return self.temp
  end

  local getF = function()
    return self.temp * 1.8 + 32
  end

  local getK = function()
    return self.temp + 273.15
  end

  return {                       
    setC = setC,                  
    getC = getC,                   
    getF = getF,
    getK = getK
  }
end

return Temp

And here is my main method:

temp = require "tempClass"

io.write("Please enter the initial temperature: ")
usrTemp = io.read("*n")

myTemp = temp:new(usrTemp)

print("The current temperature in Celsius is: ".. myTemp:getC())
print("The current temperature in Fahrenheit is: " .. myTemp:getF())
print("The current temperature in Kelvin is: " .. myTemp:getK())
io.write("Please enter new temperature: ")
changeTemp = io.read("*n")
myTemp:setC(changeTemp)
print("The current temperature in Celsius is: " .. myTemp:getC())
print("The current temperature in Fahrenheit is: " .. myTemp:getF())
print("The current temperature in Kelvin is: " .. myTemp:getK())
io.write("Please enter new temperature: ")

My problem is the if usrTmp < -273.15 then line in the setC function. I'm getting this error message:

lua: ./tempClass.lua:10: attempt to compare table with number
stack traceback:
    ./tempClass.lua:10: in function 'setC'
    [string "<eval>"]:14: in main chunk

I know, however, that usrTmp is a number. If I call type on the variable before the function, I get type number. In the function, the type is table. Why is usrTmp a table in the function? How can I fix this? Thanks!

Upvotes: 1

Views: 64

Answers (1)

lubgr
lubgr

Reputation: 38277

You need to be explicit about the self parameter when defining functions that shall be used with it. The function setC should have an additional such parameter:

local setC = function(self, usrTmp)
  -- as before...
 end

Recall that these two invocations are identical:

myTemp:setC(changeTemp)
myTemp.setC(myTemp, changeTemp)

That should explain the actual error message your received.


In addition, you need to turn Table.new into an ordinary (not self-parameter-enhanced) function. It's not connected to an instance yet, it is supposed to return one. And finally, the state variable temp must be included in the table that Table.new returns:

function Temp.new(tmp)
  --         ^ note the dot instead of the colon

  -- function body as before, but all functions now need the self parameter, e.g.:
  local getC = function(self)
     return self.temp
  end


  return {
    temp = self.temp,
    setC = setC,
    getC = getC,
    getF = getF,
    getK = getK
  }
end

Upvotes: 2

Related Questions