Property discussion wrap-up

TommiT tommitissari at hotmail.com
Thu Jan 31 07:18:35 PST 2013


On Thursday, 31 January 2013 at 14:47:17 UTC, TommiT wrote:
> [..]

Although, it's just not very healthy to be passing those property 
variables around, because it enables writing all kinds of bugs:

struct S
{
     int n;

     property Prop
     {
         @property int get() { return outer.n; }
         alias this = get;

         Prop opAssign(string S : "+")(int v)
         {
             outer.n += v;
             return this;
         }
     }
     Prop prop;
}

void foo(T)(T t)
     if (isImplicitlyConvertible!(T,int))
{
     auto v = t;
     v += 1; // calls t.prop.opAssign!"+"(1)
             // given: is(T == S.Prop)
}

...

S s;

foo(s.prop); // increments S.n, not good

So, I guess properties need to be magic, they're not imaginable 
as a some kind of restricted + augmented structs. The crux of the 
matter is the alias this which is needed for implementing the 
getter (accessor) of the property. The property variable should 
*not* be convertible to getter, but the property variable itself 
should be the getter.


More information about the Digitalmars-d mailing list