lua-users home
lua-l archive

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

Dylan Cuthbert wrote:
Of course now I have a massive overhead on all uses of "=" to variables
within object :-(

Hi Dylan,

Note that the overhead is only when *new* keys are assigned to the table. So unfortunately if you assign another value to willy3 it won't work the way you want it to. Try this:

  object.nadgers.willy3 = function() bum = 90 end
  object.nadgers.willy3 = function() bum = 90 end

You can also use nicer syntax in Lua 5.0:
  function object.nadgers.willy3() bum = 90 end

A Lua function will have the same environment as where it was defined,
so I recommend implementing some kind of package system (See the list archive, I posted some references not long ago).
This will also help you protect the default globals.

Try something like:

local function cache(tab, name)
  local value = _G[name]
  rawset(tab, name, value)  -- cache value
  return value

function loadpkg(filename)
  local P = {}
  setmetatable(P, {__index = cache})
  -- setmetatable(P, {__index = _G}) -- if you don't want cache
  local f = assert(loadfile(filename))
  setfenv(f, P)
  return P, f()

- Peter Shook