lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2014-05-01 19:50 GMT+02:00 Thomas Jericke <tjericke@indel.ch>:

> I am not so sure if I would implement it this way. Just as a Gedankenexperiment,
> if methods would be first class citizen of Lua it would mean that the function itself
> would know that it is a method or a static function.
>
> So if you write:
> table = {}
> function table:f ()
>   ....
> end
>
> f would contain a internal reference to table. So local b = table.f would contain that
> reference as well and b() would call f with table as first parameter.

That's a totally different paradigm. The colon in Lua is sugar for
sugar for sugar.

function table:f (...)

is sugar for

function table.f (self,...)

which is sugar for

table.f = function(self,...)

which is sugar for

table["f"] = function(self,...)

So f receives a local reference to the object, not a reference to the prototype.