Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 22, 2018 Is it possible to exit a contract? | ||||
---|---|---|---|---|
| ||||
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? |
March 22, 2018 Re: Is it possible to exit a contract? | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | 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
|
March 22, 2018 Re: Is it possible to exit a contract? | ||||
---|---|---|---|---|
| ||||
Posted in reply to berni | 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
|
March 22, 2018 Re: Is it possible to exit a contract? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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...
|
Copyright © 1999-2021 by the D Language Foundation