Continued looking at properties in D - interfaces and constraints

mikey via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 9 04:54:50 PDT 2016


Hi,

I'm continuing to look at properties in D and have found another 
area where I think there may be an issue - or maybe where I'm 
doing something wrong.

I have started trying to use constraints on my properties to 
constrain which values they can take I have also started trying 
to use interfaces. What I noticed was that when I combine these 2 
features the constraints get discarded.

     interface Widthy {
         @property inout(int) width() inout;
         @property void width(int width);
     }

     class Test : Widthy {
     private:
         int _w;
     public:
         @property inout(int) width() inout { return _w; }
         @property void width(int width)
         in {
             import std.exception;
             if (width < 0) {
                 throw new
                 Exception("width is less than zero");
             }
         }
         body {
             _w = width;
         }
     }

     void main() {
         import std.stdio;
         auto t = new Test;
         t.width = -1;
         writeln("width: ", t.width);
         // width: -1

         // hmmm... not good
     }


More information about the Digitalmars-d-learn mailing list