[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: _ENV vs _G
- From: Tim Hill <drtimhill@...>
- Date: Wed, 13 Aug 2014 14:50:08 -0700
On Aug 13, 2014, at 1:34 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2014-08-13 20:00 GMT+02:00 Tim Hill <drtimhill@gmail.com>:
>> On Aug 13, 2014, at 4:55 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> [1] Binary chunks can have _ENV parameters, so (a) is possible.
>>
>>
>> Interesting, how do you do that?
>
> $ lua
> Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
>> f=function(_ENV) return _ENV,id end
>> cf=string.dump(f)
>> io.open("f.luac","wb"):write(cf):close()
>>
> $ lua
> Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
>> f=loadfile"f.luac"
>> print(_ENV)
> table: 0x1f205f0
>> return f{id="my_env"}
> table: 0x1f440d0 my_env
>
Ah, interesting border-case. In fact, even if you DO specify an environment to loadfile() in this case it will be inaccessible to the function.
I’ve not really given it much thought, but a function that declares _ENV as one of its arguments in fact has NO access to the global environment at all so far as I can see (except indirectly via upvalues or other arguments, both of which the caller can control). Is this, then, a Lua idiom for writing (partially) pure functions? With the obvious caveat that such a function is free to call impure functions.
—Tim
- References:
- _ENV vs _G, Mason Mackaman
- Re: _ENV vs _G, Steven Degutis
- Re: _ENV vs _G, Roberto Ierusalimschy
- Re: _ENV vs _G, Tim Hill
- Re: _ENV vs _G, Roberto Ierusalimschy
- Re: _ENV vs _G, Steven Degutis
- Re: _ENV vs _G, Dirk Laurie
- Re: _ENV vs _G, Tim Hill
- Re: _ENV vs _G, Dirk Laurie
- Re: _ENV vs _G, Tim Hill
- Re: _ENV vs _G, Dirk Laurie
- Re: _ENV vs _G, Tim Hill
- Re: _ENV vs _G, Dirk Laurie
- Re: _ENV vs _G, Tim Hill
- Re: _ENV vs _G, Dirk Laurie