Possible @property compromise

TommiT tommitissari at hotmail.com
Wed Jan 30 13:25:23 PST 2013


On Wednesday, 30 January 2013 at 21:07:56 UTC, TommiT wrote:
> The problem is that genie is out of the bottle once you have 
> committed yourself to an interface where there is a public 
> member variable. And you can't put the genie back in the bottle 
> any more. This means that your interface can't add any 
> encapsulation over this public member variable afterwards 
> without changing the interface.

A bit more code, a bit less metaphors:

struct S
{
     T data;
}

// end-user code:

S s;
*&s.data = 42;

End-user thus by-passes any encapsulation that could be possible 
added over S.data field later on, like the following:

struct S
{
     private T _data;

     @property ref T data() { return _data; }

     @property ref T data(int v)
     {
         assert(v != 42);
         _data = v;
         return _data;
     }
}


More information about the Digitalmars-d mailing list