"Iain Buclaw" <ibuc...@ubuntu.com> wrote in message news:i9p9li$282...@digitalmars.com... >A few standard library functions, such as 'abort' and 'exit', cannot >return. > However there is no way in DMD to let the compiler know about this. > Currently in D2, you must either have a 'return' or 'assert(0)' statement > at > the end of a function body. It would be nice however if you can give hints > to > the compiler to let it know that a function is never going to return. > > Example: > > @noreturn void fatal() > { > print("Error"); > exit(1); > } > > The 'noreturn' keyword would tell the compiler that 'fatal' cannot return, > and > can then optimise without regard to what would happen if 'fatal' ever did > return. This should also allow fatal to be used instead of a return or > assert > statement. > > Example: > > int mycheck(int x) > { > if (x > 1) > return OK; > fatal(); > } > > > Thoughts?
A while ago someone mentioned a feature that some languages have that's a specific that indicates "never returns". (I'll just call it type "noreturn"). One of the nice things about that is you don't have to provide a "fake" return type. For instance, with your "@noreturn": "@noreturn int foo()" would be legal, but wouldn't make any sence. And in a way, even "@noreturn void foo()" isn't great since a "void" return value suggests that it at least returns. So your "fatal" would be like this: noreturn fatal() { print("Error"); exit(1); } And likewise, "exit" would be declared as "noreturn exit(int)" (otherwise the compiler would, presumably, complain that a "noreturn" function returns). Since this "noreturn" would be an actual type, in the langauges that have it, it will often participate in arithmetic types, so you can have a function that may or may not return: Arithmetic!(noreturn, int) foo(int num) { if(num == 7) exit(1); else return num; } Or something like that anyway. (Although, AIUI, those languages generally have arithmetic types built into the langauge itself.)