www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Untested return values.

reply Lionello Lunesu <lio lunesu.remove.com> writes:
What would happen if DMD would issue a compiler warning for untested 
return values?

bool somefunc() { ... }

void main() {
   somefunc();//warning: untested return value
   auto b = somefunc();//OK
}

Especially useful for C or OS functions returning error values or handles.

L.

PS. might even be worth a compiler error.
Oct 03 2006
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Lionello Lunesu wrote:
 What would happen if DMD would issue a compiler warning for untested 
 return values?
 
 bool somefunc() { ... }
 
 void main() {
   somefunc();//warning: untested return value
   auto b = somefunc();//OK
 }
 
 Especially useful for C or OS functions returning error values or handles.
 
 L.
 
 PS. might even be worth a compiler error.
I regularly use functions I don't need the return values of, so I wouldn't like this change. On the other hand, Walter might stop using printf() all over Phobos... :P Nah, not worth it. Besides, the fact that functions like printf() have a return value is probably enough reason for Walter not to make such a change.
Oct 03 2006
next sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Frits van Bommel wrote:
 Lionello Lunesu wrote:
 What would happen if DMD would issue a compiler warning for untested 
 return values?

 bool somefunc() { ... }

 void main() {
   somefunc();//warning: untested return value
   auto b = somefunc();//OK
 }

 Especially useful for C or OS functions returning error values or 
 handles.

 L.

 PS. might even be worth a compiler error.
I regularly use functions I don't need the return values of, so I wouldn't like this change. On the other hand, Walter might stop using printf() all over Phobos... :P Nah, not worth it. Besides, the fact that functions like printf() have a return value is probably enough reason for Walter not to make such a change.
LOL, really? What does printf return? (what about writef?)
Oct 03 2006
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Hasan Aljudy wrote:
 Frits van Bommel wrote:
 Lionello Lunesu wrote:
 What would happen if DMD would issue a compiler warning for untested 
 return values?

 bool somefunc() { ... }

 void main() {
   somefunc();//warning: untested return value
   auto b = somefunc();//OK
 }

 Especially useful for C or OS functions returning error values or 
 handles.

 L.

 PS. might even be worth a compiler error.
I regularly use functions I don't need the return values of, so I wouldn't like this change. On the other hand, Walter might stop using printf() all over Phobos... :P Nah, not worth it. Besides, the fact that functions like printf() have a return value is probably enough reason for Walter not to make such a change.
LOL, really? What does printf return? (what about writef?)
from http://www.cplusplus.com/ref/cstdio/printf.html (at bottom): ----------- *Return Value.* On success, the total number of characters printed is returned. On error, a negative number is returned. ----------- I suppose it could be used if want to align the next output to a specific column without fixing all the fields to specific lengths. Though the %n specifier could also be used for that[1]. [1] If you don't know what it does, don't worry. It's another obscure fact of printf(): %n accepts an int* that's filled with the number of characters printed so far. I believe it's main use is in format string exploits :P.
Oct 03 2006
prev sibling next sibling parent reply Walter Bright <newshound digitalmars.com> writes:
Frits van Bommel wrote:
 Lionello Lunesu wrote:
 What would happen if DMD would issue a compiler warning for untested 
 return values?
I regularly use functions I don't need the return values of, so I wouldn't like this change. On the other hand, Walter might stop using printf() all over Phobos... :P Nah, not worth it. Besides, the fact that functions like printf() have a return value is probably enough reason for Walter not to make such a change.
I tried this back in the '80s. It's one of those things that sure sounds like a good idea. The problem is, there are just too many cases where the return value is legitimately ignored (like printf's), so you wind up filling your code with chaff like: cast(void) printf( ... ); After a few dozen of those, it starts looking like a bad idea <g>.
Oct 03 2006
parent reply "Andrei Khropov" <andkhropov nospam_mtu-net.ru> writes:
Walter Bright wrote:

 I tried this back in the '80s. It's one of those things that sure sounds like
 a good idea. The problem is, there are just too many cases where the return
 value is legitimately ignored (like printf's), so you wind up filling your
 code with chaff like:
 
 	cast(void) printf( ... );
 
 After a few dozen of those, it starts looking like a bad idea <g>.
Well, it depends on a particular syntax. In Nemerle for example it's just " _ = SomeFunc();" -- AKhropov
Oct 04 2006
parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
Andrei Khropov wrote:
 Walter Bright wrote:
 
 I tried this back in the '80s. It's one of those things that sure sounds like
 a good idea. The problem is, there are just too many cases where the return
 value is legitimately ignored (like printf's), so you wind up filling your
 code with chaff like:

 	cast(void) printf( ... );

 After a few dozen of those, it starts looking like a bad idea <g>.
Well, it depends on a particular syntax. In Nemerle for example it's just " _ = SomeFunc();"
That's true! How about: "void = printf()", similar to the void initializer "int[] x = void;" L.
Oct 05 2006
parent reply Kristian <kjkilpi gmail.com> writes:
On Thu, 05 Oct 2006 12:26:11 +0300, Lionello Lunesu  =

<lio lunesu.remove.com> wrote:

 Andrei Khropov wrote:
 Walter Bright wrote:

 I tried this back in the '80s. It's one of those things that sure  =
 sounds like
 a good idea. The problem is, there are just too many cases where the=
=
 return
 value is legitimately ignored (like printf's), so you wind up fillin=
g =
 your
 code with chaff like:

 	cast(void) printf( ... );

 After a few dozen of those, it starts looking like a bad idea <g>.
Well, it depends on a particular syntax. In Nemerle for example it's=
=
 just  " _ =3D SomeFunc();"
That's true! How about: "void =3D printf()", similar to the void =
 initializer "int[] x =3D void;"

 L.
Well, I ignore return values of functions a lot. For example, 'eraseFile()' returns true if a file was deleted or false o= n = failure. Usually I don't care what the return value is: if the file = couldn't be deleted, too bad, there is not much that I can do about it = (except possibly notify the user by opening a message box, but that's no= t = always wanted). The return value of such functions is a 'side effect', not the main thin= g. = Lets say that these functions belong to a group A. There are also = functions whose 'main thing' is their return value. There may or may not= = be some 'side effects' when they are called. These functions belong to a= = group B. Clearly you wouldn't want to do any extra work when calling functions of= = the group A. Or have warning/error messages with them. However, with the= = group B that would be ok. One solution could be to tell the compiler that a function belongs to th= e = group B. For example: pure double sqrt(double val); void func() { double x; sqrt(2.0); //error x =3D sqrt(2.0); //ok } Notice a new keyword 'pure'.
Oct 05 2006
parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
Kristian wrote:
 On Thu, 05 Oct 2006 12:26:11 +0300, Lionello Lunesu 
 <lio lunesu.remove.com> wrote:
 
 Andrei Khropov wrote:
 Walter Bright wrote:

 I tried this back in the '80s. It's one of those things that sure 
 sounds like
 a good idea. The problem is, there are just too many cases where the 
 return
 value is legitimately ignored (like printf's), so you wind up 
 filling your
 code with chaff like:

     cast(void) printf( ... );

 After a few dozen of those, it starts looking like a bad idea <g>.
Well, it depends on a particular syntax. In Nemerle for example it's just " _ = SomeFunc();"
That's true! How about: "void = printf()", similar to the void initializer "int[] x = void;" L.
Well, I ignore return values of functions a lot. For example, 'eraseFile()' returns true if a file was deleted or false on failure. Usually I don't care what the return value is: if the file couldn't be deleted, too bad, there is not much that I can do about it (except possibly notify the user by opening a message box, but that's not always wanted).
If you don't care whether the file got deleted, why do you delete it in the first place? OK, I know very well what you're trying to say and I do it myself all the time, but I'll also admit that it's sloppy programming. Furthermore, 'the D way' is to throw an exception, not to return some value that is or is not tested. By the way, do you often use try { /* some code*/ } catch (Object o) {/* no code*/} too? They should, since the same reasoning applies to function throwing an exception.
 The return value of such functions is a 'side effect', not the main 
 thing. Lets say that these functions belong to a group A. There are also 
 functions whose 'main thing' is their return value. There may or may not 
 be some 'side effects' when they are called. These functions belong to a 
 group B.
eraseFile returning 'false' is not a side-effect, it is the main thing not being done. A side effect would be printf returning the number of characters written.
 Clearly you wouldn't want to do any extra work when calling functions of 
 the group A. Or have warning/error messages with them. However, with the 
 group B that would be ok.
Granted, so perhaps it could be a warning message after all. Definately not an error, we've established that.
 One solution could be to tell the compiler that a function belongs to 
 the group B. For example:
 
 pure double sqrt(double val);
 
 void func() {
     double x;
 
     sqrt(2.0);      //error
     x = sqrt(2.0);  //ok
 }
 
 Notice a new keyword 'pure'.
I like that. It's actually enabling that specific warning/error message on a per function basis. Functions returning handles to resources or fatal return values (mostly C libraries probably) could be tagged with this identifier and the compiler will then make sure the return value is actually used... This is like a contract, in a way, for the caller of the function (as opposed to contact for the implementor.) L.
Oct 05 2006
parent reply Kristian <kjkilpi gmail.com> writes:
On Thu, 05 Oct 2006 14:45:42 +0300, Lionello Lunesu  
<lio lunesu.remove.com> wrote:
 Kristian wrote:
[snip]
 If you don't care whether the file got deleted, why do you delete it in  
 the first place? OK, I know very well what you're trying to say and I do  
 it myself all the time, but I'll also admit that it's sloppy programming.

 Furthermore, 'the D way' is to throw an exception, not to return some  
 value that is or is not tested.

 By the way, do you often use

 try { /* some code*/ } catch (Object o) {/* no code*/}

 too? They should, since the same reasoning applies to function throwing  
 an exception.
Ok, ok, maybe my 'eraseFile()' example wasn't the best one... :) I can't resist (sorry) pointing out that sometimes it's ok to silently ignore errors. ;) For example, there is a program that creates a temporary file. When the program is exited, the file will be deleted. Now, if the file cannot be deleted for some reason, what could you do? Notify the user with a message box telling that "Cannot delete the XXX file." or "Please delete the XXX file, or else!..." (that would annoy me, at least), or silently accept the fact that the file cannot be deleted and exit the program? *grin* (Thanks for pointing out the usage of exceptions in D, though.)
 Clearly you wouldn't want to do any extra work when calling functions  
 of the group A. Or have warning/error messages with them. However, with  
 the group B that would be ok.
Granted, so perhaps it could be a warning message after all. Definately not an error, we've established that.
A warning message with an ability to turn it off? Or as I would prefer, a warning with an ability turn it on (it's off by default)?
Oct 05 2006
parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
Kristian wrote:
 On Thu, 05 Oct 2006 14:45:42 +0300, Lionello Lunesu 
 <lio lunesu.remove.com> wrote:
 Kristian wrote:
[snip]
 If you don't care whether the file got deleted, why do you delete it 
 in the first place? OK, I know very well what you're trying to say and 
 I do it myself all the time, but I'll also admit that it's sloppy 
 programming.

 Furthermore, 'the D way' is to throw an exception, not to return some 
 value that is or is not tested.

 By the way, do you often use

 try { /* some code*/ } catch (Object o) {/* no code*/}

 too? They should, since the same reasoning applies to function 
 throwing an exception.
Ok, ok, maybe my 'eraseFile()' example wasn't the best one... :) I can't resist (sorry) pointing out that sometimes it's ok to silently ignore errors. ;) For example, there is a program that creates a temporary file. When the program is exited, the file will be deleted. Now, if the file cannot be deleted for some reason, what could you do? Notify the user with a message box telling that "Cannot delete the XXX file." or "Please delete the XXX file, or else!..." (that would annoy me, at least), or silently accept the fact that the file cannot be deleted and exit the program? *grin*
Like I said: point taken :)
 Clearly you wouldn't want to do any extra work when calling functions 
 of the group A. Or have warning/error messages with them. However, 
 with the group B that would be ok.
Granted, so perhaps it could be a warning message after all. Definately not an error, we've established that.
A warning message with an ability to turn it off? Or as I would prefer, a warning with an ability turn it on (it's off by default)?
In D, warnings are off by default. You explicitly ask for warnings using -w. L.
Oct 05 2006
parent =?ISO-8859-15?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lionello Lunesu wrote:

 A warning message with an ability to turn it off? Or as I would 
 prefer, a warning with an ability turn it on (it's off by default)?
In D, warnings are off by default. You explicitly ask for warnings using -w.
Also, in D *all* warnings are errors. So the "-w" flag to DMD is the same as passing "-Wall -Werror" to GCC when compiling C/C++ sources. --anders
Oct 05 2006
prev sibling parent reply "Lionello Lunesu" <lionello lunesu.remove.com> writes:
"Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message 
news:eftv4g$2ueh$1 digitaldaemon.com...
 Lionello Lunesu wrote:
 What would happen if DMD would issue a compiler warning for untested 
 return values?

 bool somefunc() { ... }

 void main() {
   somefunc();//warning: untested return value
   auto b = somefunc();//OK
 }

 Especially useful for C or OS functions returning error values or 
 handles.

 L.

 PS. might even be worth a compiler error.
I regularly use functions I don't need the return values of, so I wouldn't like this change. On the other hand, Walter might stop using printf() all over Phobos... :P Nah, not worth it. Besides, the fact that functions like printf() have a return value is probably enough reason for Walter not to make such a change.
Damn that printf :S I actually use the printf in D quite a lot, but only because it's there. If writefln would be imported by default I'd use that instead. L.
Oct 03 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lionello Lunesu wrote:

 I actually use the printf in D quite a lot, but only because it's there.If 
 writefln would be imported by default I'd use that instead.
You're in good company then, the language's author agrees with you :-) Seriously, it's in the wrong header. And it's pining for the fjords... --anders
Oct 03 2006
parent reply Walter Bright <newshound digitalmars.com> writes:
Anders F Björklund wrote:
 Seriously, it's in the wrong header. And it's pining for the fjords...
It just moved.
Oct 04 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Walter Bright wrote:

 It just moved.
OK, so there might be a "import std.c.stdio;" to insert in a couple of files to make them compile ? Much better now than after the initial D release... This should also help promote writef, which is good. Thanks Walter, --anders
Oct 04 2006
parent reply Walter Bright <newshound digitalmars.com> writes:
Anders F Björklund wrote:
 Walter Bright wrote:
 
 It just moved.
OK, so there might be a "import std.c.stdio;" to insert in a couple of files to make them compile ?
I mean the parrot moved <g>.
Oct 04 2006
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Walter Bright wrote:

 OK, so there might be a "import std.c.stdio;" to
 insert in a couple of files to make them compile ?
I mean the parrot moved <g>.
No, it didn't, that was you hitting the cage! ;-) Too bad. --anders
Oct 04 2006
prev sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Walter Bright wrote:
 Anders F Björklund wrote:
 Walter Bright wrote:

 It just moved.
OK, so there might be a "import std.c.stdio;" to insert in a couple of files to make them compile ?
I mean the parrot moved <g>.
what parrot?
Oct 04 2006
parent reply Pragma <ericanderton yahoo.removeme.com> writes:
Hasan Aljudy wrote:
 
 
 Walter Bright wrote:
 Anders F Björklund wrote:
 Walter Bright wrote:

 It just moved.
OK, so there might be a "import std.c.stdio;" to insert in a couple of files to make them compile ?
I mean the parrot moved <g>.
what parrot?
This parrot: http://www.mtholyoke.edu/~ebarnes/python/dead-parrot.htm IMO, it's right up there with "Who's on First", although Palin and Cleese's delivery on the small screen is far more hilarious than what you'll see on the page. And now for something completely different. -- - EricAnderton at yahoo
Oct 04 2006
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Pragma wrote:

 This parrot:
 
 http://www.mtholyoke.edu/~ebarnes/python/dead-parrot.htm
See also: http://www.digitalmars.com/d/archives/digitalmars/D/bugs/5838.html
 And now for something completely different.
Indeed. Those sketches are for another programming language. :-) --anders
Oct 04 2006