Possible @property compromise

TommiT tommitissari at hotmail.com
Wed Jan 30 04:04:30 PST 2013


On Wednesday, 30 January 2013 at 02:15:09 UTC, Rob T wrote:
> Even with @property restrictions, I still don't think it will 
> work as expected. For example, if the variable is a struct, 
> then you have to disallow operations on the struct from outside.
>
> Example:
>
> struct Y { int a; }
>
> struct X{ @property Y y; }
>
> X x;
>
> x.y.a = 4; // <- this has to be illegal!
>
> Reason?
>
> struct X{
>
>   Y _y;
>
>   @property Y y{ return _y; }
>
> }
>
> // this won't change _y as it did before.
> x.y.a = 4;

Somehow I had missed this post. So, we can bury the idea of 
restricting the access to a public member variable by making it 
illegal to take the address of it. And as long as it is possible 
to take the address of a public member variable, it is possible 
for the end-user to by-pass all encapsulation that you might 
later on add over that public member variable. For this reason, 
my logic says, it is impossible to invent such an implementation 
of property concept that would make it possible to first put in a 
public member variable and later on encapsulate it *without* 
changing the interface.


More information about the Digitalmars-d mailing list