lua-users home
lua-l archive

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

On Fri, Jul 19, 2013 at 4:02 PM, Javier Guerra Giraldez
<> wrote:
> local f = o:m
> as syntax sugar to:
> local f = function(...) return o.m(o, ...)  end

That would be a good addition too.

> i think it's better than Greg's proposal because it doesn't magically
> changes the meaning of an existing operator (dot)

That's not exactly fair.  It magically changes the dot operator in the
same way the __index metamethod magically changes the dot operator.
Both are tucked under the hood.  From the user's perspective, the same
code could have just as easily been written with no metamethods at

local funcs = {
    inc = function(self)
        self.a = self.a + 1
        return self

local function new(t)
   local o = t or {a = 0}
   for k,f in pairs(funcs) do
      o[k] = function(...) return f(o, ...) end
   return o

assert(new().inc().inc().inc().a == 3)

Using the __index metamethod is a handy way of exposing a Lua library
with the familiar syntax of JavaScript, Python, Java, etc.  Using this
proposed __method metamethod is a technique for getting that syntax
without the performance regression.