[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: 'with' statement
- From: Karel Tuma <kat@...>
- Date: Wed, 09 Mar 2016 18:14:27 +0100
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.