lua-users home
lua-l archive

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


Although I haven't tested it, I believe this would make the function :method syntax work:

(Original almost-diff: http://lua-users.org/lists/lua-l/2008-01/msg00525.html)

static void funcfield (LexState *ls, struct ConsControl *cc) {
 FuncState *fs = ls->fs;
 int reg = fs->freereg;
 expdesc key, val;
 int rkkey;
+  int needself;
 checknext(ls, TK_FUNCTION);
+  needself = testnext(ls, ':');
 checkname(ls, &key);
 luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
 cc->nh++;
 rkkey = luaK_exp2RK(fs, &key);
- body(ls, &val, 1 /* set to 0 to disable implicit self */, ls->linenumber);
+  body(ls, &val, needself, ls->linenumber);
luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));
 fs->freereg = reg;
}

And commas can be made optional following inline functions without ambiguouity (the patch does indeed make them optional). They can't with the current means of declaring a function though:

tab = {
 field = (function() end) "test",
 field = function() end or function() end,
 etc = ...
}

- Alex

----- Original Message ----- From: "Mark Hamburg" <mhamburg@adobe.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Friday, February 22, 2008 7:07 AM
Subject: Re: Function definitions in table constructors


on 2/21/08 1:39 PM, Duncan Cross at duncan.cross@gmail.com wrote:

On Thu, Feb 21, 2008 at 2:00 PM, Alex Davies <alex.mania@iinet.net.au> wrote:
It's quite an easy change to the parser, see:
 http://lua-users.org/lists/lua-l/2008-01/msg00525.html

The patch is a little different though - for my oo system I prefer to have
 implicit selfs inside table functions,

Perhaps the best solution would be to combine this with a second,
independent change that takes:

   function :anonmethod(a,b,c)
      ...
   end

as syntactic sugar for:

   anonmethod = function(self,a,b,c)
      ...
   end

so table-constructor methods have a lightweight syntax.

But to really make it pay off one needs some way to also remove the need for
a comma after the end.

Mark