Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is [function class:me() end] equal to [function class.me(self) end] in lua?

I am having a little hard time to understand the colon operator in lua. So I did some experiment on colon operator and came up with this conclusion.

class = {}

function class:me()
end

is equal to

function class.me(self)
end

Am I making a right conclusion?

If not, what is the problem?

like image 783
wonjun cho Avatar asked Nov 20 '25 00:11

wonjun cho


1 Answers

Your conclusion is correct.

The : form is just syntactic sugar for the . form with self as the first parameter. This is documented:

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement

 function t.a.b.c:f (params) body end

is syntactic sugar for

 t.a.b.c.f = function (self, params) body end
like image 68
tonypdmtr Avatar answered Nov 22 '25 17:11

tonypdmtr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!