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:

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");
 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->, 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" <>
To: "Lua list" <>
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 wrote:

On Thu, Feb 21, 2008 at 2:00 PM, Alex Davies <> wrote:
It's quite an easy change to the parser, see:

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)

as syntactic sugar for:

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

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.