lua-users home
lua-l archive

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


This is a great idea, and would work perfectly if I hadn't stupidly allowed multiple inheritence. Maybe I'll just go with single inheritence and use your suggestion. Thanks

David Anderson wrote:

Quoting John Paquin <jpaquin@breakawaygames.com>:

Have any of you guys dealt with the problem of using a class inheritence structure and trying to reload scripts on the fly?

I have tackled and solved a problem very similar to yours recently, by using a
"dynamic proxy" table. It works exactly like a normal pure proxy, except that
you can repoint the proxy to any table whenever you need to. And because the
whole application works with references to the proxy table, they immediately
start using the new backend table when it changes.

With (a lot of) help from Rici Lake, I have put together these functions for
creating and modifying a dynamic proxy table:
--
do
 local dynamic = setmetatable({}, {
   __mode = "k",
   __index = function (_,k) error("dynamicProxy expected, got " .. type(k), 3) end
 })
 function dynamic:__index (k) return dynamic[self][k] end
 function dynamic:__newindex (k,v) dynamic[self][k] = v end
 local function setAndReturnKey (t, key, val) t[key] = val; return key end

 function DynamicProxy(tab, old)
   return setAndReturnKey (dynamic, setmetatable(old or {}, dynamic), tab)
 end

 function CopyDynamicProxy(src, dst)
   return setAndReturnKey (dynamic, dst or DynamicProxy({}), dynamic[src])
 end
end
--

Note that the use of this dynamic proxy requires some care when localising
variables: local meth = dynproxy.method will keep a non-dynamic reference. In
some cases you'll want that, so you can execute a block and be sure the methods
won't shift beneath your feet, but if you're not careful you can end up with
very confusing bugs because you are hanging on to an obsolete method instead of
resolving the new one through the dynamic proxy.

If its of any use, I have also written a reimport function to go with LTN-11's
import, that reloads a LTN-11 package (plus all its dependancies) and repoints
the public interfaces to the new packages.

Hope this helps a little.
David Anderson

-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/