lua-users home
lua-l archive

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

I've just discovered Lua, and I'm impressed. Waow! What a
great, great job! Congratulations to Roberto, Waldemar,
Luiz and all the others who contributed to Lua.

While discovering it, some questions and comments arise -
maybe have they already been addressed, in which case
I apologize in advance.

Here it is:

This is an excerpt from the "Lua 5.1 Reference Manual":
    "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"

I wonder why a new ':' operator was added?

Wouldn't have been easier to push silently an extra
reference onto the stack for every function call, this
reference being accessible (read-only?) as "self"
(or "_self"?) in any function body?

Obviously, this "self" could be nil, when a function is
invoked without any "var" prefix (e.g. global function).

Of course there is the drawback to systematically push this
information onto the stack, but it seems not that bad,
considering we could have:
- More straightforward syntax (it saves the need for an
extra operator)
- More standard usage (most languages use a "." notation
to invoke a method -- see
- Ability to access the "var" the function call was applied to, from the body of any function, even in non OO contexts.

Best regards,