Contracts with interface

tchaloupka via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 19 03:33:10 PDT 2015


This bites me again:

import std.stdio;

interface ITest
{
     void test();

     void test2()
     in { writeln("itest2"); }

     void test3()
     in { writeln("itest3"); }

     void test4()
     in { writeln("itest4"); assert(false); }
}

class Test: ITest
{
     void test()
     in { writeln("ctest"); }
     body { }

     void test2()
     {
     }

     void test3()
     in { writeln("ctest3"); }
     body {}

     void test4()
     in { writeln("ctest4"); }
     body {}
}

void main()
{
     auto t = new Test();
     t.test();
     t.test2();
     t.test3();
     t.test4();
}

What is expected output?

Docs says just:
> Interface member functions can have contracts even though there 
> is no body for the function. The contracts are inherited by any 
> class member function that implements that interface member 
> function.

and:
> If a function in a derived class overrides a function in its 
> super class, then only one of the in contracts of the function 
> and its base functions must be satisfied. Overriding functions 
> then becomes a process of loosening the in contracts.
> A function without an in contract means that any values of the 
> function parameters are allowed. This implies that if any 
> function in an inheritance hierarchy has no in contract, then 
> in contracts on functions overriding it have no useful effect.

What I expected is, that if there is no contract in interface and 
is in class implementation - it will be called. Or if interface 
has contract and class implementation doesn't, it will be called 
too.

But apparently it works the way that you have to have the same IN 
contract in both interface and class implementation to be safe. 
So it works the same way as with class inheritance per docs.
Which seems at least to me a bit strange and not much usable. 
What's the point of defining contract in interface just to write 
it again in the implementation class?
It's simpler to just write it in the class method body and not 
use the IN contracts at all.
At least a warning would be nice.


More information about the Digitalmars-d-learn mailing list