I wished I had a way to error on unused return value for a type

N

not_a_commie

Some of my classes are immutable. Calling any function on one of those
classes without using its return value is an error. I wished I could
declare this as an attribute of the function itself that would then
cause a compiler error if that return value was not used. Has anybody
else had this need?
 
F

Family Tree Mike

But there are many IO functions (I believe) that return things like the
number of bytes written. In many cases, ignoring the value is fine. Are you
only talking about a case like the line below being flaged?

string s = "This string";
s.Insert(5, "bad "); // <- flag this line?

Wouldn't something like FxCop pick this up, and isn't that sufficient?
 
J

Jeroen Mostert

not_a_commie said:
Some of my classes are immutable. Calling any function on one of those
classes without using its return value is an error. I wished I could
declare this as an attribute of the function itself that would then
cause a compiler error if that return value was not used. Has anybody
else had this need?

Not really. There are so many things that could potentially be checked at
compile time, but we can't keep extending the compiler...

If this is really important to you (more important than, say, the writers of
the String class considered it) you could try using FxCop
(http://msdn2.microsoft.com/library/bb429476) and write custom rules for
detecting this situation. A modified version of FxCop is part of some
editions of Visual Studio as Code Analysis, and custom FxCop rules can be
used there too (see, for example,
http://blogs.msdn.com/fxcop/archive...sual-Studio_3F00_-_5B00_David-Kean_5D00_.aspx).
 
L

Lasse Vågsæther Karlsen

not_a_commie said:
Some of my classes are immutable. Calling any function on one of those
classes without using its return value is an error. I wished I could
declare this as an attribute of the function itself that would then
cause a compiler error if that return value was not used. Has anybody
else had this need?

Why is it an error?

I understand your question, and the reason behind it, but at least once
I have had to call a function that had no side-effects and disregard its
return value, just to make sure the object on which I called the method
was in a valid state.

In other words, there might be other reasons for calling a method on the
object than to obtain information from it.

What in particular makes your particular method call an error?

Besides, what specifically do you mean by "not used"?

Would the following be considered "not used"?

- store the return value into a local variable in the method
- store the return value into a field in the class
- use the return value in an expression, even if the result of that
expression isn't saved anywhere?
 
I

Israel

Besides, what specifically do you mean by "not used"?

That's a good question because I could just pass it to a method that
does nothing with it also.

In all of the time that I've used C++ and C# I've had this same
thought maybe once or twice because I called some method that returned
an object whose lifecycle was now being turned over to the caller and
I forgot to capture it. In all of those cases I always found it
better to rewrite the original method and make it an out parameter so
as not to get burned by that. In general I prefer being able to just
ignore return values when I don't care about them and if I don't "use"
them there shouldn't be any type of memory or resource leak (which is
harder to do accidentally with a GC).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top