[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: explicit mode
- From: Duane Leslie <parakleta@...>
- Date: Wed, 11 May 2016 12:26:19 +1000
> 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.
- References:
- explicit mode, Viacheslav Usov
- Re: explicit mode, Luiz Henrique de Figueiredo
- Re: explicit mode, Viacheslav Usov
- Re: explicit mode, Chris Berardi
- Re: explicit mode, Viacheslav Usov
- Re: explicit mode, Michal Kottman
- Re: explicit mode, Viacheslav Usov
- Re: explicit mode, Soni L.
- Re: explicit mode, Sean Conner
- Re: explicit mode, Soni L.
- Re: explicit mode, Sean Conner
- Re: explicit mode, Soni L.