lua-users home
lua-l archive

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


> No <> = old handling, and adding it to the outer function doesn't force you to add it to inner functions. The old handling means it'll try to fetch _ENV from the enclosing scope but if there's none, it'll act like an implicit <> (assuming no other upvalues would be used).

Yes, the absence of any markup should always be the backwards compatible behaviour.  You can also explicitly get the old behaviour with the markup `<...>`.

Another interesting thing is that because of the way the `_ENV` variable is treated specially you can actually pass the environment through a "restricted" function.

```lua
cache = {}
local function scoped(globals) <> -- No _ENV
  return (function (_ENV)
    local print, cache = print, cache
    return function(key) <print, cache>
      print("DEBUG: key is ", key)
      return cache[key]
    end
  end)(globals)
end
```

Then calling `scoped(_ENV)` returns the function which has a copy of the global `print` and `cache` objects as its upvalues.  Essentially once there exists any variable or upvalue or argument with the name `_ENV` it becomes the global scope.  Then the local declaration promotes the globals to locals which are then available as upvalues to the returned function.

Regards,

Duane.