Lua class not working

278 Views Asked by At

I have a simple class implementation in Lua.

test = {}
test.__index = test

function test:new()
    local o = {}
    setmetatable(o, self)
    return o
end

function test:setName(name)
    self.name = name
    print name
end

local name = test:new()
name:setName("hello")

I keep getting this error when I run it:

lua: test.lua:12: '=' expected near 'name'

I not sure what or why this happening, any help would be greatly appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

Change print name to print(name). print is just a regular function and function calls need parentheses unless they are called with a single argument that is either a string literal or a table literal.