Continued looking at properties in D - interfaces and constraints

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 12 14:43:14 PDT 2016


On 10/12/2016 10:49 PM, mikey wrote:
>     import std.exception;
>
>     interface Widthy {
>         @property inout(int) width() inout;
>         @property void width(int width) in { enforce(width < 0); }
>     }
>
>     class Test : Widthy {
>     private:
>         int _w;
>     public:
>         @property inout(int) width() inout { return _w; }
>         @property void width(int width) in { enforce(width < 0); }
>         body {
>             _w = width;
>         }
>     }
>
>     void main() {
>         import std.stdio;
>         auto t = new Test;
>         t.width = -1;
>         writeln("width: ", t.width);
>         // width: -1
>
>         // doesn't look right
>     }

The contract of the interface seems to see a wrong value:

----
import std.stdio;

interface Widthy {
     void width(int width)
     in {
         writeln(width); /* prints "2" */
         assert(width > 0);
     }
}

class Test : Widthy {
     void width(int width) in { assert(width > 0); } body {}
}

void main() {
     auto t = new Test;
     t.width(-1);
}
----

I haven't checked closely, but there's issue 15984 which is titled 
"Interface contracts retrieve garbage instead of parameters". That's 
probably it. It's a regression in 2.071.

https://issues.dlang.org/show_bug.cgi?id=15984


More information about the Digitalmars-d-learn mailing list