Question about contracts on methods.

Don nospam at nospam.com
Mon Nov 30 07:27:20 PST 2009


Peter C. Chapin wrote:
> Lutger <lutger.blijdestijn at gmail.com> wrote in
> news:hescc2$161$1 at digitalmars.com: 
> 
>> You are not missing something, this is a known issue. It has been
>> discussed and I believe the intention was to do something about this,
>> but with all the high priorities I'm not sure when this will be
>> solved. 
> 
> Okay, thanks for the information. It occurs to me that one way to solve 
> this problem would be to allow declarations in the precondition be 
> visible in the post condition. Then one could do something like
> 
>     void next()
>     in {
>         int original_day_m   = day_m;
>         int original_month_m = month_m;
>         int original_year_m  = year_m;
>     }
>     out {
>         assert( ... expression using original_day_m, etc ... )
>     }
>     body {
>       // No code related to pre or post conditions.
>     }
> 
> This might not be an ideal resolution but it seems a lot better than the 
> current situation and it doesn't look like it would be too hard to 
> implement (but what do I know!).

The problem is, where do those variables get stored? The function body 
will overwrite anything that's left on the stack.
At the moment, AFAIK the only way to do this at present is to write the 
contracts as nested functions.

void next()
{
      int original_day_m;
      int original_month_m;
      int original_year_m;
     void inContract() { original_day_m = day_m; ...}
     inContract();
     void outContract() { assert( ... expression using original_day_m, 
etc ... ) }

     // body...
    outContract();
    return;
}


More information about the Digitalmars-d-learn mailing list