www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - contracts in interfaces: do they work, do I have to do something to

reply Emil <emilper gmail.com> writes:
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);
     }
}
Oct 01 2018
parent Alex <sascha.orlov gmail.com> writes:
On Monday, 1 October 2018 at 13:49:53 UTC, Emil wrote:
 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);
     }
 }
Yeah... had such problems too, a while ago. https://forum.dlang.org/thread/sadsceubvapbeezjytdp forum.dlang.org Seems, that one of the tickets is closed, but the pull request is not merged. Don't know how matters stand with this, actually.
Oct 01 2018