This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
superaxander's profile picture

Function with : notation error

Started by superaxander, 23 January 2015 - 04:52 PM
superaxander #1
Posted 23 January 2015 - 05:52 PM
I am using the following code:

-snip-
Line: 9	  newObject.__index = newObject
Line: 10	   local function newObject:get(valueName)
Line: 11	   return self[valueName]
Line: 12    end
-snip-
And I am getting the error:

[string "luaoop"]:10: '(' expected
Am I missing something obvious?
Full code can be found at: https://github.com/superaxander/LuaOOP/raw/master/luaoop.lua
valithor #2
Posted 23 January 2015 - 05:57 PM
edit: was wrong
Edited on 23 January 2015 - 05:04 PM
wieselkatze #3
Posted 23 January 2015 - 06:01 PM
Using ':' is perfectly fine. You just can't make that function local as it is created inside your table. Using

function newObject:get( valueName )
should work.
superaxander #4
Posted 23 January 2015 - 06:04 PM
In the code shown on: http://lua-users.org...ntationTutorial

local MyClass = {}
MyClass.__index = MyClass
setmetatable(MyClass, {
  __call = function (cls, ...)
	return cls.new(...)
  end,
})
function MyClass.new(init)
  local self = setmetatable({}, MyClass)
  self.value = init
  return self
end
-- the : syntax here causes a "self" arg to be implicitly added before any other args
function MyClass:set_value(newval)
  self.value = newval
end
function MyClass:get_value()
  return self.value
end
local instance = MyClass(5)
they are clearly using it when defining the function. I'll try and see if it can be used without the : when declaring and only using it while using the function.
EDIT:
Using ':' is perfectly fine. You just can't make that function local as it is created inside your table. Using

function newObject:get( valueName )
should work.
Ah thanks for the quick response. Cheers!
Edited on 23 January 2015 - 05:04 PM