lua-users home
lua-l archive

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


David Jeske wrote:
> 
> Below is a real example of a class (cut and pasted right out of my
> game project) which inherits from three 'trait/mixin' style base
> classes.

> declare_class("MainShip", {
>         _parents = { air_physics, controllable, collidable },
> 
>         Condition = "Healthy",
>         imgdir = 2.0,   -- the image index
>[...]
>         -- constructor
>         new = function (self,a_list) -- constructor
>[...]
>         end,
> 
>         recharge = function (self,byWhom) -- recharge method
>[...]
>         end
> }); -- register done

> Here are a few notes about why I do the things I do:
>[...] 
>   4) Personally I find the syntax ugly, and hard to read.

Just apply the attached patch (for Lua 4.0) and you can omit
the ',' in the 'name=val' constructor part and use the named
functions definitions there.  I.e:

  { a=1 b=2 function c() end d=4 }

Ciao, ET.
--- src/lparser-orig.c	Tue Jul 31 18:26:44 2001
+++ src/lparser.c	Tue Jul 31 18:35:22 2001
@@ -495,7 +495,13 @@
       check(ls, ']');
       break;
     }
-    default: luaK_error(ls, "<name> or `[' expected");
+    case TK_FUNCTION: {
+      next(ls);
+      luaK_kstr(ls, checkname(ls));
+      body(ls, 0, ls->linenumber);
+      return;
+    }
+    default: luaK_error(ls, "<name>, `function' or `[' expected");
   }
   check(ls, '=');
   exp1(ls);
@@ -507,8 +513,8 @@
   FuncState *fs = ls->fs;
   int n = 1;  /* at least one element */
   recfield(ls);
-  while (ls->t.token == ',') {
-    next(ls);
+  for (;;) {
+    optional(ls, ',');
     if (ls->t.token == ';' || ls->t.token == '}')
       break;
     recfield(ls);
@@ -550,9 +556,11 @@
       cd->k = ls->t.token;
       break;
     }
+    case TK_FUNCTION:
     case TK_NAME: {  /* may be listfields or recfields */
       lookahead(ls);
-      if (ls->lookahead.token != '=')  /* expression? */
+      /* <name> '='  or  'function' <name> */
+      if (ls->lookahead.token != '=' && ls->lookahead.token != TK_NAME)
         goto case_default;
       /* else go through to recfields */
     }