lua-users home
lua-l archive

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


2017-07-15 7:13 GMT+02:00 Petri Häkkinen <petrih3@gmail.com>:
>> On 14.7.2017, at 23.40, Dibyendu Majumdar <mobile@majumdar.org.uk> wrote:
>> Hmmm, you are comparing Lua with a statically typed language C which
>> is not a valid comparison. In C, the kind of OO I am talking about is
>> prevalent - such as take stdio. The FILE* is the object. Because of
>> static type checking a function call syntax works fine. In Lua, this
>> is errorprone as it is a dynamic language - so an API that asks users
>> to say file:write() helps the user to avoid mistakes.
> In practice though, after you have dropped the magical self notation
> and start thinking outside the realm of objects and classes, you began
> to wonder why you ever needed ':' in the first place. It begins to feel
> very artificial to have one biased function arg when you really just
> have functions operating on its args. Also not needing to shoehorn
> every operation into a class, was a real eye- opener for me.

We're off-topic, which for this partuclar topic is fine with me, and
since the OP is a major culprit, I guess it is fine with him too.

OO has its uses, but is not the cure-all solution to every problem.
What I like about OO in Lua is that you can use exactly as much
of it as you need. We've got polymorphism anyway, without any
metatables, so one of the big arguments for OO is irrelevant.
You want to separate data and methods? Use an __index
metamethod. Encapsulation, inheritance? All possible, but it
is a good idea to read "Programming in Lua" before you start.

On the other hand, if you have a userdata then OO is clearly
indicated. You have to create a method table anyway, and the
whole point of that table is to supply access methods, so there
is always one special argument.

Files are userdata. The Lua string type is for all practical purposes
a predefined userdata, so having 'string' as its __index metamethod
makes compelling sense.

Tables ... this gets gray. I sometimes feel I need OO in order to be
able to hide it. I.e. I write a polymorphic global function that does
the object-oriented calls (modelled on 'pairs', 'tostring' etc).

-- Dirk