Musings on infinite loops and not reachable returns

bearophile bearophileHUGS at lycos.com
Tue Mar 25 13:49:56 PDT 2014


This code compiles with no errors or warnings:


struct Foo {
     int opApply(int delegate(ref ubyte) dg) {
         int result;
         ubyte x;
         while (true) {
             result = dg(x);
             if (result) return result;
             x++;
         }
         //return result; // Warning: statement is not reachable
     }
}

struct Bar {
     int opApply(int delegate(ref ubyte) dg) {
         int result;
         foreach (y; Foo()) {
             result = dg(y);
             if (result) return result;
         }
         return result; // required
     }
}

void main() {}



Note how the opApply() of Foo should not end with a return, while 
the opApply() of Bar is required by the D compiler to end with a 
return.

Yet, Foo is contains an infinite loop, so the result of Bar will 
not be reached. But the type system of D is not strong enough to 
see that.

There are languages able to specify such things in their type 
system. But it's probably not worth adding this to the D type 
system (on the other hand some people have suggested to add a 
@noreturn annotation to D, that's usable to denote functions that 
never return).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list