www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - preconditions and interfaces

reply Antonio Corbi <antonio ggmail.com> writes:
Hi all,

Playing with interfaces and preconditions in methods I get 
strange results with dmd-2.0.84.0 but also with dmd-nightly. My 
code is like this:
-------------
import std.stdio;

interface Thing2D {
   void width(int w)
     in {
       writeln("Thing2D.width contract w = ",w);
       assert(w > 0);
     }
}

class Line : Thing2D {
   override void width(int w)
     in {
       writeln("Line.width contract w = ",w);
       assert(w >= 0);
     }
   do {
     writeln("Line.width: w = ", w);
   }
}

void main() {
   auto l = new Line;
   l.width(-1);
}

---------------

1) With dmd-2.084.0 I get:
./ifaceprecond
Thing2D.width contract w = 2
Line.width: w = -1

2) With dmd-nightly (as of today) I get random values for 
interface's 'w':
./ifaceprecond
Thing2D.width contract w = 647271536
Line.width: w = -1

I think this should be an error caught by the contract, isn't it?

Thx!
Antonio
Jan 20
parent Alex <sascha.orlov gmail.com> writes:
On Sunday, 20 January 2019 at 15:39:49 UTC, Antonio Corbi wrote:
 Hi all,

 Playing with interfaces and preconditions in methods I get 
 strange results with dmd-2.0.84.0 but also with dmd-nightly. My 
 code is like this:
 -------------
 import std.stdio;

 interface Thing2D {
   void width(int w)
     in {
       writeln("Thing2D.width contract w = ",w);
       assert(w > 0);
     }
 }

 class Line : Thing2D {
   override void width(int w)
     in {
       writeln("Line.width contract w = ",w);
       assert(w >= 0);
     }
   do {
     writeln("Line.width: w = ", w);
   }
 }

 void main() {
   auto l = new Line;
   l.width(-1);
 }

 ---------------

 1) With dmd-2.084.0 I get:
 ./ifaceprecond
 Thing2D.width contract w = 2
 Line.width: w = -1

 2) With dmd-nightly (as of today) I get random values for 
 interface's 'w':
 ./ifaceprecond
 Thing2D.width contract w = 647271536
 Line.width: w = -1

 I think this should be an error caught by the contract, isn't 
 it?

 Thx!
 Antonio
Seems to be something known... https://forum.dlang.org/post/hficlieevnvzrnxywkzm forum.dlang.org
Jan 20