www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D exit() function?

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
I'm kind of dumbfounded.  I can't find any kind of D-compatible exit() 
function in phobos.  Using std.c.stdlib.exit will simply end the program, 
and module dtors/gc_term won't be called.  There's nothing in std.thread 
either.

Am I missing something?  Or is this something that should probably be added 
to phobos? 
Mar 25 2007
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Jarrett Billingsley wrote:
 I'm kind of dumbfounded.  I can't find any kind of D-compatible exit() 
 function in phobos.  Using std.c.stdlib.exit will simply end the program, 
 and module dtors/gc_term won't be called.  There's nothing in std.thread 
 either.
 
 Am I missing something?  Or is this something that should probably be added 
 to phobos? 
It doesn't currently exist. However, "throw new Object" would accomplish much the same thing. Sean
Mar 25 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message 
news:eu6fvd$f5r$2 digitalmars.com...

 It doesn't currently exist.  However, "throw new Object" would accomplish 
 much the same thing.


 Sean
Except that it would print an error message, and exiting without an error is certainly a possible scenario.
Mar 25 2007
parent "Saaa" <empty needmail.com> writes:
 It doesn't currently exist.  However, "throw new Object" would accomplish 
 much the same thing.


 Sean
Except that it would print an error message, and exiting without an error is certainly a possible scenario.
Haha, so I wasn't just too dumb to find it :)
Mar 25 2007
prev sibling parent reply Krzysztof =?UTF-8?B?U3p1a2llxYJvasSH?= <krzysztof.szukielojc gmail.com> writes:
Jarrett Billingsley wrote:

 I'm kind of dumbfounded.  I can't find any kind of D-compatible exit()
 function in phobos.  Using std.c.stdlib.exit will simply end the program,
 and module dtors/gc_term won't be called.  There's nothing in std.thread
 either.
 
 Am I missing something?  Or is this something that should probably be
 added to phobos?
I think simple scope(exit) should do the trick. void exitFun(){ ... } void main(){ scope(exit) exitFun(); ... }
Mar 25 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Krzysztof Szukielojc" <krzysztof.szukielojc gmail.com> wrote in message 
news:eu6m78$m1r$1 digitalmars.com...

 I think simple scope(exit) should do the trick.

 void exitFun(){
 ...
 }

 void main(){
   scope(exit) exitFun();
   ...
 }
No, I'm not looking for an atexit() replacement, sorry. I'm looking for a way to end my D program at an arbitrarily deep call depth and still have it call the RTL cleanup functions. I suppose it would be possible to do something like: extern(C) void _cleanupD() { _moduleDtor(); gc_term(); } .. void main() { atexit(&_cleanupD); // program } But that's horribly non-portable. Something like this should be in Phobos.
Mar 25 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Jarrett Billingsley wrote:
 "Krzysztof Szukielojc" <krzysztof.szukielojc gmail.com> wrote in message 
 news:eu6m78$m1r$1 digitalmars.com...
 
 I think simple scope(exit) should do the trick.

 void exitFun(){
 ...
 }

 void main(){
   scope(exit) exitFun();
   ...
 }
No, I'm not looking for an atexit() replacement, sorry. I'm looking for a way to end my D program at an arbitrarily deep call depth and still have it call the RTL cleanup functions. I suppose it would be possible to do something like: extern(C) void _cleanupD() { _moduleDtor(); gc_term(); } ... void main() { atexit(&_cleanupD); // program } But that's horribly non-portable. Something like this should be in Phobos.
Another way of doing it, stolen from Python :P
 class SystemExit : Object {}

 int main(char[][] args)
 {
     int result = 0;
     try
     {
         result = appmain(args);
     }
     catch( SystemExit e )
     {
         // Meh.
     }
     return result;
 }

 int appmain(char[][] args)
 {
     // Your code here!
 }

 // ...

 void really_deep_function()
 {
     throw new SystemExit;
 }
This should be portable, and ensures that all dtors (class and module) and scope(exit)/scope(failure) get executed. It might take a little longer to get back up to main, but at least it should be safe :) -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 25 2007