July 26, 2021, 3:09pm
So I saw some modules on the forum using functions. And some used module:function() and others do module.function()
Why does using semi colons return a table and a period acts like a normal function?
And what would be a realistic use of using the semi colons
July 26, 2021, 3:13pm
semi colons = implicit self declaration in the args
: just carries self when you’re using tables.
. doesn’t carry self when using tables.
--if you want to access something in the main table, use :
local a = self.a --you get self
--If you're making a function and you don't want self just use .
--If you DO want self, you would have to place it as one of the arguments
July 26, 2021, 3:15pm
it should also be noted if you know the object you are getting the function to you could also do smth like this
July 26, 2021, 3:25pm
Others have already covered it, but maybe this helps too.
It’s basically just short-hand code. Some call it “syntactic sugar.”
When you call a table function with
:, it injects the table itself as the first argument:
When you define a table function with
:, it creates a hidden
self parameter as the first parameter.
function myTable.Hello(self, msg) print(self == myTable) end
function myTable:Hello(msg) print(self == myTable) end
So ideally, you match the caller and the definition. If it’s defined using
:, then it makes sense to
call it using
:. If it’s defined using
., then call it with