www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5464] New: Attribute to not ignore function result

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5464

           Summary: Attribute to not ignore function result
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



Ignoring the result of some functions like string replace(), or the C
realloc(), or in general the result of strongly pure functions in D, is a
programmer mistake that often enough is a sign of a bug presence.

To face this problem GNU C has the warn_unused_result attribute:
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
(This is more useful in C than D because in C there are no true built-in
exceptions, so error return values are common, and sometimes ignoring them is a
mistake.)

Some C lints require a void cast where you don't want to use a function result:
cast(void)foo(x);

In a language the default is different and where you don't want to use a
function result you have to add a specific annotation:
unused foo(x);

In D an attribute like  nodiscard (name invented by Andrej Mitrovic) may turn
ignoring return values into errors. To silence this error the programmer uses
something like cast(void).

So the error message may be:
"Error: unused result of  nodiscard function. Use cast(void) to override it."

Strongly pure functions may produce this error even if you don't use
 nodiscard.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 20 2011
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5464




In Phobos there are several functions that have no important effects (despite
sometimes not being pure) that are useful just for their return value, like
isSorted, front, setDifference, etc.  nodiscard is useful for them all.

See also bug 3882

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 28 2011