lua-users home
lua-l archive

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


2017-03-04 8:32 GMT+02:00 Petri Häkkinen <petrih3@gmail.com>:
> On Mar 1, 2017, at 3:10 AM, Soni L. <fakedme@gmail.com> wrote:
>
>> So, Lua has very limited OOP:
>>
>> object:method() --> object.method(object)
>>
>> But, there are other forms of OOP which are useful:
>>
>> object.system.method(object)
>> object.trait.method(object)
>>
>> These could be added to Lua as a new syntax sugar, the `:.` syntax:
>>
>> object:system.method()
>> object:trait.method()
>
> Why all the trouble? I mean, you could just use free functions:
> object_system_func(a, b, c)
>
> This is faster (no need for nested dynamic table look ups) and there is no need for "self" parameter that is magically more important than the others. Also you don't need to artificially invent new classes just because you want to add a function that does not naturally fit into your existing class hierarchy.

I agree with all you say — but since 5.2 Lua actually has a VM instruction
for object-oriented access, and it does make some kinds of code look
cleaner, especially when the object is a userdata but all the other
parameters are simple scalars.