lua-users home
lua-l archive

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


I have a new patch that allows table constructs to enclose a scope
so that variables used inside the table have special meaning.
This is intended to make some aspects of Lua's data description
less awkward, and it can be used as follows:

  local function struct(name)
    local scope = {
      int = function(name) return "int[" .. name .. "]" end;
      string = function(name) return "string[" .. name .. "]" end
    }
    return setmetatable({}, {
      __scope = scope;
      __call = function(_, t)
                 return name .. "[" .. table.concat(t, ",") .. "]"
               end
    })
  end

  local obj = struct "foo" {
                int "bar";
                string "baz";
              }
  assert(obj == "foo[int[bar],string[baz]]")
  assert(string.lower("ABC") == "abc")
  assert(int == nil)
  print "DONE"

See the full description and patch in
http://lua-users.org/wiki/TableScope .

Comments welcome.  I'm interested also in opinions from the Lua
authors concerning inclusion of such a feature (probably with a
different implementation) in Lua 5.2.