FeedbackThread: DIP 1038-- at nodiscard--Community Review Round 1

Paul Backus snarwin at gmail.com
Wed Dec 9 15:29:01 UTC 2020


On Wednesday, 9 December 2020 at 14:30:13 UTC, Andrej Mitrovic 
wrote:
>> By contrast, @nodiscard can be used with any function because 
>> all functions have a return type.
>
> Just a small implementation note: I think @nodiscard should not 
> apply to void functions. In other words, if I have:
>
> -----
> @nodiscard:
>
> int foo();
> void bar();
> -----
>
> then calling `bar();` should just work, we shouldn't need to 
> write `cast(void)bar()`.
>
> At least I can't think of use-cases for it having an effect on 
> void functions.

I agree that @nodiscard is not really useful on void functions, 
but it seems to me like the obvious solution is to just...not 
annotate void functions with @nodiscard. Unlike 
@safe/pure/nothrow/@nogc, it is not transitive and will never be 
inferred by the compiler, so the only way a void function can end 
up with @nodiscard is if the programmer explicitly annotates it 
as such.

If you can come up with a realistic example of a void function 
being annotated with @nodiscard, please reply in the Discussion 
thread.

If we want to go down the route of making certain return types 
exempt, then we also have to consider whether @nodiscard should 
apply to examples like this:

struct Void {}
@nodiscard Void baz();

It is worth noting that neither C++'s [[nodiscard]] nor Rust's 
#[must_use] make an exception for functions that return void/unit.


More information about the Digitalmars-d mailing list