lua-users home
lua-l archive

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


On Wed, Jan 23, 2013 at 10:10 AM, Huang, Baiyan
<Baiyan.Huang@morganstanley.com> wrote:
> Then I want to remove this global variable, but I want to give a buffer time
> for our user to off board, so I stage the change and firstly it will emit an
> warning if user is using this global variable.  Is there a way to achieve
> this without using LuaMacros?

Sure, but you have to attach a metatable to _G, that tracks
__newindex.  For this to fire on GLOBAL_VARIABLE this variable must
not be in _G already, so __index is defined to look it up.

Something like this:

local global_variable = 'boo'

local function message(writing)
   print 'you are still using GLOBAL_VARIABLE'
end

setmetatable(_G,{
   __index = function (t, k)
       if k == 'GLOBAL_VARIABLE' then
            message(false)
            return global_variable
        end
  end;
  __newindex = function(t,k,v)
     if k == 'GLOBAL_VARIABLE' then
         message(true)
         global_variable = v
     end
 end;
})

Easy to generalize for a whole bunch of 'globals' that you want to deprecate.

steve d.