lua-users home
lua-l archive

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


2018-07-21 18:56 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
> Even without any new construct, the idea of "emergency collections"
> is not intended to be the basic method of resource release. We still
> believe that programmers should call explicit 'close' methods after
> using a resource.  Only in exceptional circunstances (mostly when
> exceptions occurr) the regular 'close' method would not be called.
> Even then, regular garbage collection would deal with most of these
> leftovers. I would expect that in a well-written program, "emergency
> collection" would be really rare, but we have no data on this.

If the rule is, that explicit resource freeing is better than implicit, why
implement a GC at all? It's the programmers fault for not freeing memory.
And a GC is not only implicit, but it's basically an unpredictable
non-deterministic operation in a program.

I would really like if there was some (meta-)method being called upon
a "regular"
stack unwind, like "return" or "error". I would not expect a "yield" to free
my file handle. Like in C++ it would allow for some more useful implicit
operations that the programmer could formulate in his programs.

It's standard in C++ to use a std::lock_guard() to make a clear point.
To tell everyone who reads the code: "don't worry, if this routine is done,
this mutex is released". Something like that is really missing from Lua and
can't be implemented from inside Lua. I would not expect a full blown
"(unwind-protect ...)" like in Lisp here, but leaving a mark on the stack
like you suggested earlier can do the trick.

An argument would be, that it would break TCO in an implicit and/or
unexpected way if it is not explicit that things are to be closed upon
scope exit.

  function read_config(x)
      auto f = io.open(...)
      --- ...
  end

I see that it introduces a new implicit concept, that non regular
programmers might have their issues with. Is that what you worry
about?


Greetings