Re: compiler warnings for unconditional recursive calls
- From: cody <deutronium@xxxxxx>
- Date: Fri, 08 Jun 2007 01:24:02 +0200
Hey Jon, you are lightning fast as always :)
comments inline!
Jon Skeet [C# MVP] wrote:
cody <deutronium@xxxxxx> wrote:Why doesn't the compiler give me any warning if it encounters code like the following:
void foo()
{
// some code here
// foo() ist not nested in a pre-condition loop, some if- or else- // block and no return statement appears before it.
foo();
}
The compiler should recognize something like that, shouldn't it?
Well, there's nothing in the C# specification to say that it should. Your criteria wouldn't be enough anyway - what about performing *any* operation which could result in an exception (beyond running out of memory or stack)?
You are right, an exception could terminate the thing, but I wouldn't consider it good style, so a warning wouldn't do any harm (if somebody really wants he can disable this warning for this specific method).
I was slightly surprised to see the other day that this is valid too:
public Test()
: this("hello")
{
}
public Test(string x)
: this()
{
}
I believe that's forbidden in Java (I should check it, but I'm too tired) but that recursive unconditional calls aren't.
This is very funny. Why is it allowed? This way your class is better protected from instantiation then by declaring its ctors private :)
Even throwing an Exception from within the ctors doesn't help here because that code is never reached..
Even if the throw() would be executed before the constructor chaining then the Exception would also prevent instantiation.
So theoretically the Compiler should warn us:
- That the class can never be instantiated
- The be have unconditional recursion
- That we have unreachable code (when constructors are not empty)
But in reality: Not a single warning.
So you can do nothing with it, besides calling non-void static methods in the this() call:
public Test()
: this(StaticFunction("hello"))
{
}
Very interesting. If Anders Heijlson and his team thought about this case while developing the Language?
.
- Follow-Ups:
- Re: compiler warnings for unconditional recursive calls
- From: Jon Skeet [C# MVP]
- Re: compiler warnings for unconditional recursive calls
- References:
- compiler warnings for unconditional recursive calls
- From: cody
- Re: compiler warnings for unconditional recursive calls
- From: Jon Skeet [C# MVP]
- compiler warnings for unconditional recursive calls
- Prev by Date: Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
- Next by Date: Re: interfaces
- Previous by thread: Re: compiler warnings for unconditional recursive calls
- Next by thread: Re: compiler warnings for unconditional recursive calls
- Index(es):
Relevant Pages
|