www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it possible to exit a contract?

reply berni <someone somemail.de> writes:
Part of my code looks like this:

 out (result)
 {
   if (result==true)
   {
     lots of assertions...
   }
 }
I would prefer an early exit like:
 out (result)
 {
   if (result==false) return;
 
   lots of assertions...
 }
Is this possible somehow (return and break don't do the job...). The only thing I found is using goto, which I prefer over that gigantic if-block, but maybe there is something better, I don't know of?
Mar 22 2018
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/22/2018 06:16 AM, berni wrote:
 Part of my code looks like this:
 
 out (result)
 {
   if (result==true)
   {
     lots of assertions...
   }
 }
I would prefer an early exit like:
 out (result)
 {
   if (result==false) return;

   lots of assertions...
 }
Is this possible somehow (return and break don't do the job...). The only thing I found is using goto, which I prefer over that gigantic if-block, but maybe there is something better, I don't know of?
Never thought of it before. :) I would put all that code into a function and call from the out block. Ali
Mar 22 2018
parent berni <someone somemail.de> writes:
On Thursday, 22 March 2018 at 16:22:04 UTC, Ali Çehreli wrote:
 Never thought of it before. :) I would put all that code into a 
 function and call from the out block.
I rejected this, because I've got an unease feeling using extra functions in contract programming. Can't tell, where this feeling comes from; maybe just because I've too few experience with contract programming...
Mar 22 2018
prev sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Thursday, 22 March 2018 at 13:16:00 UTC, berni wrote:
 Part of my code looks like this:

 out (result)
 {
   if (result==true)
   {
     lots of assertions...
   }
 }
I would prefer an early exit like:
 out (result)
 {
   if (result==false) return;
 
   lots of assertions...
 }
Is this possible somehow (return and break don't do the job...). The only thing I found is using goto, which I prefer over that gigantic if-block, but maybe there is something better, I don't know of?
It's a hack, but it works: void bar() in {(){ return; assert(false); }();} body { } unittest { bar(); } -- Simen
Mar 22 2018