September 19, 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.
September 19, 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