There is anything like nodiscard attribute in D?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jul 29 15:16:19 UTC 2019


On Monday, July 29, 2019 8:23:43 AM MDT victoroak via Digitalmars-d-learn 
wrote:
> I have an Expected/Result struct, but without an attribute
> similar to nodiscard
> (https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
> is not very useful.
>
> There is anything like that in D? If not, there is any plan to
> add it?

No, D does not currently have a feature like that, and there are not
currently any plans to add such a feature. If someone writes a DIP (D
Improvement Proposal) for such a feature and provides good enough arguments
for why it's worth adding that the DIP ends up being accepted, then such a
feature would be added, but AFAIK, no one has written such a proposal.

The closest that D has to such a feature is that if a function is pure, then
IIRC in at lesat some cases, the compiler will warn if the return value is
ignored, because there isn't normally any point to a pure function if you
ignore everything that it does, but that probably requires that the function
be "strongly" pure (meaning that all of its parameters are immutable or
implicitly convertible to immutable), since if it's possible for any effects
to escape via the function arguments (e.g. because they're pointers or ref),
then ignoring the return value isn't necessarily a problem. However, that's
the only time that D complains about a function's return value being
ignored.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list