lua-users home
lua-l archive

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


Excerpts from Soni L.'s message of 2016-03-09 11:17:52 +0100:

> local function with(t, e)
>    return setmetatable({}, {__index=function(t,k) return t[k] or e[k] end})
I'd advise just for:

do local _ENV = with_table -- hey, it reads as english too!
  ...
end

The idea is that:
1) You should never use globals in the do body, everything you need should
   come from upper closure (ie bunch of caching local = in file header)
2) "with" table statement is the only indexing we should ever do,
   so _ENV is appropiate

I think this is one of the major of uses of _ENV.
Note that it should not be overused. Usually one needs it for "table initializers"
where we need to excute full statements and mere expressions would be clumsy.