lua-users home
lua-l archive

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

Currently, Lua uses the _ENV upvalue for the environment. It'd be
interesting if there was an _GETNAME upvalue that behaves like __index
but for every value and only when passed a name.

Do be careful not to use _GETNAME in the _GETNAME obviously, e.g.:

local _GETNAME = function(o, k) ... end -- good, cannot call itself
local function _GETNAME(o, k) return o[k] end -- okay, doesn't call itself
local function _GETNAME(o, k) if type(o) == "number" then ... end return
o[k] end -- BAD, calls itself (unless `type` is a local)

- This would make Lua heckin' slow!
- On a more serious note, it'd be a clean and effective way of handling
"extension methods". These are locally-scoped methods that can be
accessed through objects, as in `foo:bar()`, and unlike monkey-patching,
they shadow the object's indexes without interference with other
modules/objects. Note that o[k] syntax explicitly wouldn't call _GETNAME
- chances are the k is external to the module, so you don't want any
conflicts between the indexes and the extension methods.