lua-users home
lua-l archive

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




On Sun, Jun 23, 2019 at 3:54 PM Soni "They/Them" L. <fakedme@gmail.com> wrote:


On 2019-06-23 5:20 p.m., v wrote:
> On Sun, 2019-06-23 at 17:13 -0300, Soni "They/Them" L. wrote:
> > I do wonder how <toclose> handles nil, and why it's a bug for
> > <toclose>
> > to close a non-closeable value.
> >
> > consider:
> >
> > local <toclose> f, err = io.open(foo)
> > if not f then return nil, err end -- error: attempt to close
> > non-closeable value
>
> https://www.lua.org/work/doc/manual.html#3.3.8:
> > If the value is nil, it is ignored; otherwise, if it does not have a
> __close metamethod, an error is raised.
>
> I think that in your case error is raised because `err` is marked at
> toclose as well and it is a string (can't say for sure).
> I have to admit that it's really inconvenient for this use case.
>

ooh. I see. I didn't realize that. (the code was hypothetical, and so
was the error, sorry.)

still wish toclose was mutable (or at least nil-able) tho. for
cancelling the closing.

A reference wrapper would be trivial to write. Depending on your use case, having the object itself know whether or not it should be closed (perhaps by invoking a method on it or setting a value) is another option.

/s/ Adam