lua-users home
lua-l archive

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


2018-07-18 17:55 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> On 18 July 2018 at 15:20, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> > I did not get your point here. Why a user that would forget to properly
>> > qualify its 'local' would not forget to use 'with'? What are the
>> > differences between 'local scoped' vs 'with' except the syntax?
>>
>> it's precisely the syntax.   a 'with' block is always an extra block,
>> even if it covers the whole loop or function block (it usually
>> doesn't).  it's very visible, and so hard to miss.   if a novice
>> programmer sees the `with open('fname') as file: xxxx`  and likes it,
>> they will probably use it very intentionally.
>
> It is not by chance that Lua avoids too many syntactical constructs.
> They are hard to be represented in the C API.
>
> A good use I see for "local scoped" is in C functions, which often have
> a hard time to properly free resources. With something like "local scoped",
> it would be enough one single function to mark a stack position as
> a scoped variable, to be finalized when the function exits.

At the Lua level, maybe

with(local_variable_or_upvalue[,level])

where 'level' behaves as in 'error'?