lua-users home
lua-l archive

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


On Thu, Jun 20, 2019 at 9:18 PM Soni "They/Them" L. <fakedme@gmail.com> wrote:
>
>
>
> On 2019-06-20 4:06 p.m., Hugo Musso Gualandi wrote:
> local <with> foo = bar --> calls bar's __enter metamethod?
> end --> calls bar's __exit metamethod?
>
> this also has the benefit of handling nested scopes as well, I guess?

I think it's not need and has several problems. 1st, bar can be an
expression, and in my normal use cases it will be, normally tings like
file.open("whatever") or similar.

Also, __enter__ functionality can be easily made by things like
self-returning method, and it makes the code more explicit and easier
to read, i.e. things like "local <toclose> p = ref_counter:ref() (
where in my use case, ref_counter:ref incrementes self:ref_count,
ref_counter:unref decrements it and calls self:no_refs() if it goes to
zero, and __close calls unref )

Also, Lua has nested scopes IIRC, that's what do-end is for, it's even
documented as useful to control variable scope.

Francisco Olarte.


>