lua-users home
lua-l archive

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


Pascal introduced the construction

with tbl do
...
end

Inside the do block, fields of 'tbl' shadow outer variables.

This is hard to mimic exactly in Lua. Here is my attempt.

--- for _ENV in with(tbl,context) do ... end
--  * mimics Pascal `with`, i.e. not a real loop, just a do-once
--  * falls back to `context` which defaults to `_ENV`
--  * can be nested, outer level fields only visible if explicit
--  * temporarily deactivates metatable of `tbl`
local
function with(tbl,context)
  local t=tbl
  if type(t)~='table' then error("'with' expects a table, got "..type(t),2) end
  local m=getmetatable(t)
  return function()
    if t then
      t=nil
      return setmetatable(tbl,{__index=context or _ENV})
    else
      setmetatable(tbl,m)
    end
  end
end

Problem 1: Local variables defined outside the `for` stay visible.
Problem 2: I can't think of a neat way to retain the metatable of `tbl`.