lua-users home
lua-l archive

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


On Wed, Aug 8, 2018 at 1:49 AM Dirk Laurie <dirk.laurie@gmail.com> wrote:
Op Di., 7 Aug. 2018 om 23:10 het Gé Weijers <ge@weijers.org> geskryf:
>
> Why not throw an error? (e.g. luaL_error(L, "libtls version too old, version = 0x%x", version);)
>
> A regular return will register the module in the module table, for every value possible it seems. The error handling will bypass that.

You don't want to bypass that.

The return value 'false' is an idiom that does not prevent 'require'
from trying to reload a module, but at the same time allows your
program to test whether there has been a previous unsuccessful
attempt.

I don't see a whole lot of use for this, if a module fails to load retrying is not going to help much in almost all use cases, except where you're loading components on demand. In that case I would suggest writing a wrapper around 'require' to get the behavior needed.

One downside I see is that the reason for the error is not reported, 'false' does not tell you much, and 'require' returns only one value. If there's one thing I try to avoid is programs that fail silently.

Another downside of returning 'false' is that you have to test the return value of each 'require', which just clutters up modules, and in the case that 'require' can't find the module it'll throw an error anyway. This creates an interface that reports failures in two different ways, which makes the code handling the error more complicated.


--
--