Discussion Thread: DIP 1038-- at nodiscard--Community Review Round 1
Dennis
dkorpel at gmail.com
Mon Dec 14 14:58:23 UTC 2020
On Wednesday, 9 December 2020 at 10:10:40 UTC, Mike Parker wrote:
> This is the discussion thread for the first round of Community
> Review of DIP 1038, "@nodiscard":
This is a reply to Andrej Mitrovic's post in the Feedback Thread.
https://forum.dlang.org/post/gdjclwuwuoyqiftdercu@forum.dlang.org
> I think @nodiscard should not apply to void functions.
> (...)
> At least I can't think of use-cases for it having an effect on
> void functions.
There is a pattern that replaces `goto fail;` and `goto success;`
style error handling with calls to (inner) functions in a return
statement. It looks something like this:
```
struct Parser {
S result;
string error;
void setError(string msg) {
error = msg;
}
void parse(string s) {
if (s.length == 0) {
return setError("empty input");
}
if (!s.starsWith("MAGIC")) {
return setError("wrong header");
}
// etc.
}
}
```
This pattern is used in dmd:
https://github.com/dlang/dmd/blob/97aa2ae5ee19ce6a2979ca1627479df713f99252/src/dmd/expressionsem.d#L2524
https://github.com/dlang/dmd/blob/97aa2ae5ee19ce6a2979ca1627479df713f99252/src/dmd/expressionsem.d#L5383
Functions setError() yes() and no() should be called in a return
statement, continuing afterwards could be considered a bug. One
might want to apply @nodiscard to those functions so it will be
caught by the compiler when you forget to prepend `return`.
For that to work however, @nodiscard should not have a special
case for `void`.
More information about the Digitalmars-d
mailing list