Broken contract programing

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Wed May 13 05:05:47 PDT 2015


On 05/13/2015 12:51 PM, iackhtak wrote:
> There was discussion about broken contract programing. One broken
> thing was "in" contract within inheritance. If you add different
> "in"-contract in overridden parent and derived function only one
> will be checked.

No, this is incorrect. Only one needs to pass and if one does not, the 
other is checked as well.

class C{
     int foo()in{}out(r){assert(r<=10);}body{
         return 3;
     }
}
class D: C{
     override int foo()in{assert(false);}out(r){assert(r>=10);}body{
         return 10;
     }
}

void main(){
     D d=new D();
     d.foo();
}

What is the problem?

The problem you may have read about is that if no contract is specified 
for an overriding function, the parent contract is automatically 
loosened completely:

class C{
     void foo()in{assert(false);}body{}
}
class D: C{
     override void foo(){}
}

void main(){
     D d=new D();
     d.foo(); // ok
}




More information about the Digitalmars-d mailing list