Jump to content




Function with : notation error


  • You cannot reply to this topic
3 replies to this topic

#1 superaxander

  • Members
  • 609 posts
  • LocationHolland

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/s...ster/luaoop.lua

#2 valithor

  • Members
  • 1,053 posts

Posted 23 January 2015 - 05:57 PM

edit: was wrong

Edited by valithor, 23 January 2015 - 06:04 PM.


#3 wieselkatze

  • Members
  • 221 posts
  • LocationGermany

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.

#4 superaxander

  • Members
  • 609 posts
  • LocationHolland

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:

View Postwieselkatze, on 23 January 2015 - 06:01 PM, said:

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 by superaxander, 23 January 2015 - 06:04 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users