lua-users home
lua-l archive

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


2006/9/18 Hugh O'Byrne <hobyrne@gmail.com>:
> On 9/15/06, Jerome Vuarand <jerome.vuarand@ubisoft.com> wrote:
>>
>> You can try something like that :
>>
>> -- Start of Lua ----------------------------
>> with = function(context)
>> ...
>> end
>>
>> endwith = function()
>> ...
>> end
>>
>> -- You can then write
>> with (object) do
>>    name=parent.name..name
>> end
>> endwith()
>>
>> -- End of Lua ------------------------------
>>
>> Globals can still be accessed directly if not overriden in object, or
>> through _G inconditionnaly. Endwith is the only syntactic drawback, you have
>> to explictly restore the environment. As the do/end is not necessary, you
>> can just write:
>>
>> with (object)
>>    name=parent.name..name
>>    endwith()
>
> Even that could be encapsulated.  Something like:
>
> function withblock (context, function)
>   with (context)
>   function()
>   endwith()
> end
>
> used like:
>
> withblock (object, function () name = parent.name..name end)
>
> - Syntax may be a bit rusty, but it's possible to automate the restoration
> of the environment in this way.  I guess maybe the identifier 'function' is
> not allowed within function, there may be some other caveats too.
>
> Hugh O'Byrne.
>
>

I'm resurecting an old thread (thanks to Hugh's mail about
netiquette), but the original poster request is now nicely solved by
Lua 5.2 (the keyword being 'in' rather than 'with').