Contracts with interface

BBasile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 19 13:23:44 PDT 2015


On Saturday, 19 September 2015 at 10:33:12 UTC, tchaloupka wrote:
> 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:
>> [...]
>
> and:
>> [...]
>
> 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.

This a bug:

https://issues.dlang.org/show_bug.cgi?id=7517
https://issues.dlang.org/show_bug.cgi?id=12321

the problem aalso exist for without using an interface but with 
simple base & derived class.

https://issues.dlang.org/show_bug.cgi?id=6856
https://github.com/D-Programming-Language/dmd/pull/4200


More information about the Digitalmars-d-learn mailing list