lua-users home
lua-l archive

Module metatable

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


Hi folks,
In loadlib.c, ll_module attributes a new metatable to a module. Wouldn't it be
nicer to check for a previously assigned MT first?
--- loadlib.c.orig 2005年02月20日 21:57:34.386838440 -0500
+++ loadlib.c 2005年02月20日 22:12:10.984575328 -0500
@@ -397,7 +397,7 @@
 lua_pop(L, 1);
 else { /* no; initialize it */
 lua_pop(L, 1);
- lua_newtable(L); /* create new metatable */
+ if (!lua_getmetatable(L, -1)) lua_newtable(L); /* push metatable */
 lua_pushvalue(L, LUA_GLOBALSINDEX);
 lua_setfield(L, -2, "__index"); /* mt.__index = _G */
 lua_setmetatable(L, -2);
It's usually helpful to use module() as a class constructor instead of
module.new(), and that requires a __call metamethod. Can't think of any other
good motive, but, methinks, checking is always better anyway. :)
Cheers,
--luis
-- 
A mathematician is a device for turning coffee into theorems.
 -- P. Erdos 
-- 
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <carvalho@dam.brown.edu>

Attachment: pgptIU_cPdbFp.pgp
Description: PGP signature


AltStyle によって変換されたページ (->オリジナル) /