contracts in interfaces: do they work, do I have to do something to enable checking of contracts ?

Emil emilper at gmail.com
Mon Oct 1 13:49:53 UTC 2018


I am trying my hand at contracts and they work fine in plain 
functions and in methods, but I can't make them work in 
interfaces.

https://dlang.org/spec/interface.html#interface-contracts

$ dmd --version
DMD64 D Compiler v2.081.1
Copyright (C) 1999-2018 by The D Language Foundation, All Rights 
Reserved written by Walter Bright


/////////////////////////
void main()
{
     WillBloop test = new WillBloop();
     // also tried
     // Blooper test = new WillBloop();
     // it compiles but the in contract does not seem enforced
     test.limited(-1); // this does not croak

     auto no_interface = new NoInterface();
     no_interface.limited(-10); // this works as expected
}

interface Blooper
{
     void limited(int some_other_name)
     in
     {
         assert(some_other_name > 0, "asssert failed in 
interface");
     }
}

class WillBloop : Blooper
{
     void limited(int a_name)    {
         import std.stdio : writeln;
         writeln(a_name);
     }
}

class NoInterface
{
     void limited(int another_name)
     in
     {
         assert(another_name > 0, "assert failed in NoInterface");
     }
     do
     {
         import std.stdio: writeln;
         writeln(another_name);
     }
}


More information about the Digitalmars-d-learn mailing list