Alternatives to exceptions for error handling

Dominikus Dittes Scherkl dominikus at scherkl.de
Mon Nov 23 12:07:55 UTC 2020


On Monday, 23 November 2020 at 02:27:22 UTC, Adam D. Ruppe wrote:
> On Monday, 23 November 2020 at 02:14:33 UTC, Vladimir Panteleev 
> wrote:
>> Some valid patterns were affected though, such as returning 
>> `this` (as in the builder pattern). Perhaps it would make more 
>> sense to instead allow annotating functions as that it's safe 
>> to discard their return value (@discardable or such)?

> I currently lean toward @nodiscard as being easier to use but I 
> do expect @discardable would work pretty well too, just with a 
> more involved transition period.

At least pure functions should never be @discardable.
So discarding the return value of a pure function should always 
be an error.

At my work we use the annotation "noSideeffects" (in C), which is 
pretty much the same as pure I think, except the poor syntax 
(need to repeat the function name in a separate macro line).
Depending on this an error is set if the return value is 
discarded. Works like a charm.


More information about the Digitalmars-d mailing list