|
|
||
|
duck wrote: ...
I recently wrote a Lua function like this: local function process(self,conn)self.mutex:lock() self.count = self.count+1--some more housekeeping stuff self.mutex:unlock() self.handle(self,conn) conn:close()self.mutex:lock()--some more housekeeping stuff self.count = self.count-1 self.mutex:unlock() end
nice and clear, i agree.i've sometimes thought about a "with" construction (implemented using setfenv, i suppose). That would allow
local function process(self,conn)
with self do
mutex:lock()
count = count+1
--some more housekeeping stuff
mutex:unlock()
handle(self,conn)
conn:close()
mutex:lock()
--some more housekeeping stuff
count = count-1
mutex:unlock()
end
end
i don't think "with" should go in lua core, but it is syntactically
neat. setfenv does the job, but the restore to previous environment is
not as tidy.
what about a block-local extension to setfenv() ? at index 0 maybe.
eg
a = {count = 1}
do
setfenv(0,a)
count = count + 1 -- a's count
end
-- environment is back to previous
Adrian