Re: compiler warnings for unconditional recursive calls

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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?
.



Relevant Pages

  • Could not load file or assembly every few days - asp.net 1.1
    ... Could not load file or assembly 'Foo, Version=1.0.2388.18427, ... An unhandled exception occurred during the execution of the ... Please review the stack trace for more information about ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Augmenting Types
    ... ActiveX object). ... Not in the case of ActiveX instantiation (which is what we were ... So if the screenshot was not of an error message generated by ... whether the exception was catchable; ...
    (comp.lang.javascript)
  • Re: Having to "print" before method invocation?
    ... Hey Fredrik, thanks for responding. ... foo that I've been using without much fuss for a few months now. ... if I add print statements before trying to invoke methods on ... the interpreter won't raise the exception immediately (since it expected you to ...
    (comp.lang.python)
  • Destructors and exceptions
    ... locals appears to be deferred to program exit. ... The only rationale I can think of is to speed up exception ... class Foo: ... def premature_exit: ...
    (comp.lang.python)
  • Exceptions or logging?
    ... The instantiation retrieves various files and processes a lot of try catch ... or just ammends single properties for any failed validations. ... If my new MyProperties() throws an exception, ...
    (comp.lang.java.programmer)