Is the Compiler Wrong? - Part II
Here’s a simpler example of the previous post. This won’t compile either. Once again, the compiler reports that not all code paths return a value. Is it wrong?
string ThisWillNotCompile(bool input) { if (input) return "Hello, world."; if (!input) return "Howdy, world."; }
Comments
Comment from Andy
Date: June 19, 2009, 8:31 am
Hi Brent,
Consider if you put the following line between the 2 if’s,
input = true;
Obviously, in this case, the compiler is correct, and if you pass in false to this function, nothing will be returned.
My guess is that the compiler is returning the error because it is only checking the syntax of conditional statement, and not the correctness of the code.
if you changed the if (!input) to an else, the compiler would be happy.
That’s just my 2 cents
Andy
Write a comment