digitalmars.D.learn - preconditions and interfaces
- Antonio Corbi (40/40) Jan 20 2019 Hi all,
- Alex (3/44) Jan 20 2019 Seems to be something known...
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 2019
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! AntonioSeems to be something known... https://forum.dlang.org/post/hficlieevnvzrnxywkzm forum.dlang.org
Jan 20 2019