[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How can a module intentionally fail to load?
- From: Dirk Laurie <dirk.laurie@...>
- Date: Tue, 7 Aug 2018 09:26:20 +0200
2018-08-07 8:38 GMT+02:00 Daurnimator <email@example.com>:
> On 7 August 2018 at 16:05, Dirk Laurie <firstname.lastname@example.org> wrote:
>> Inside that 'if', write a message on io.stderr and return false.
> Please don't!
> This will manifest as e.g. a user reporting an error about trying to
> index 'false'.
> When they try and run code such as:
> tls = require "org.conman.nfl.tls"
> function dothething()
> -- at some point later in response to user input:
> It's better to fail fast and keep the error message close to the cause!
I don't agree. Returning 'false' is a Lua idiom.
> But also, writing to stderr from a library is an antipattern: please
> never do this.
> I might e.g. just be checking to see if your module is available and do:
> pcall(require, "yourmodule")
> I don't want output appearing in the user's terminal when I do that!
I'll concede that this is a plausible reason for not writing to stderr,
although If I were that user, I would not minded the hint about some
feature that the program would have exploited if available.
But this second reason nullifies the first. The purpose of returning
'false' is precisely so that a pcall will catch the error.