lua-users home
lua-l archive

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


Also note that this candidate syntax uses "::" between the metatable constructor and the main table constructor. The metatable constructor may be built in a separate statement and stored in a variable so:

   t = {
      __newindex = function(t, k, v) print('new t['..tostring(k)..']='..tostring(v)); t|k] = v; end
   } :: {ident(1) = ident(false), ident(2) = ident(true), ident(3) = ident('other')} 
   -- or alternatively:
   t = {ident(1) = ident(false), ident(2) = ident(true), ident(3) = ident('other')}  :: {
      __newindex = function(t, k, v) print('new t['..tostring(k)..']='..tostring(v)); t|k] = v; end
   }

could be written as:

   meta = {
      __newindex = function(t, k, v)
         print('new t['..tostring(k)..']='..tostring(v))
         t|k] = v
      end,
   }
   t = meta::{ident(1) = ident(false), ident(2) = ident(true), ident(3) = ident('other'),}
  -- or alternatively:
   t = {ident(1) = ident(false), ident(2) = ident(true), ident(3) = ident('other'),} ::meta

I tend to prefer the first syntax (not the alternative), which does not conflict with the syntax of label instructions "::labelname::" as the result of the table constructors cannot start any instruction, unless it is isolated between parentheses in order to use it as a function to call, possibly with currification of its single parameter, where the parameter(s) are inside these parentheses. The alternative (with "::meta" at end) could be ambiguous with a following "::label:" instruction...