lua-users home
lua-l archive

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


On Wed, Aug 13, 2014 at 2:50 PM, Tim Hill <drtimhill@gmail.com> wrote:
>
> 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

It feels to me more like a "with" block than any sort of functional idiom.

/s/ Adam