I was wandering what the . and : do/differences e.g.
[code]
Something.SomeOtherThing()
Something():SomeOtherThing()
[/code]
I believe one returns something, and the other just executes it.
[QUOTE=mikeym;25602364]I believe one returns something, and the other just executes it.[/QUOTE]
Both can return stuff its basicly the same.
So there's no reason someone would use one over the other ?
Certain things only accept . over :
[QUOTE=pyroboytodumax;25602303]I was wandering what the . and : do/differences e.g.
[code]
Something.SomeOtherThing()
Something():SomeOtherThing()
[/code][/QUOTE]
The difference depends on what the function does and how it was defined.
First off, 'Something' and 'Something()' are two totally different things. The first one you have there [i]is[/i] a table. Something.SomeOtherThing() would be calling a function in that table.
Something() is a function, which presumably [i]returns[/i] a table in this case. The ':' denotes the following as being a method, which is actually just a function which automatically has a 'self' argument representing the table that it's a member of.
This demonstrates your first line:
[lua]
local Something = {}
function Something.SomeOtherThing()
print("We're calling a function from a table!")
end
[/lua]
And here's your second line:
[lua]
local someTable = {}
function someTable:SomeOtherThing()
print( "We are calling a method from "..tostring(self).."!" )
end
local function Something()
return someTable
end[/lua]
Here's another way of writing it to better demonstrate how methods work:
[lua]
local someTable = {}
someTable.SomeOtherThing = function( self )
print( "We are calling a method from "..tostring(self).."!" )
end
local function Something()
return someTable
end[/lua]
But I'm guessing you didn't mean Something() as a function in your second line, so to avoid confusion:
[lua]
local Something = {}
function Something:SomeOtherThing()
print( "We are calling a method from "..tostring(self).."!" )
end[/lua]
[editline]23rd October 2010[/editline]
Basically, Something.SomethingElse() would be the same as calling Something:SomethingElse(), but if it was defined as a method, calling Something.SomethingElse() would not pass 'self' as an argument. If the function uses self in it, calling Something.SomethingElse() will likely spit an error back at you.
Ok. Thanks :D
Sorry, you need to Log In to post a reply to this thread.