Property discussion wrap-up

TommiT tommitissari at hotmail.com
Wed Jan 30 18:10:50 PST 2013


I think my former unholy union of namespace and variable is just 
too confusing to reason about, so here's my new way of seeing 
effectively the same thing, but with simpler semantics:

Keyword property is like the keyword struct, except:

1) properties can't have data members
2) properties can't have static member functions
3) properties can't have constructors/destructors
4) property instances always point to null
5) property instances' size is 0
6) if a property is declared inside a class/struct, all its
    member functions receive an implicit 'outer' variable
    which references the encapsulating object. Whether or
    not 'outer' is const ref or ref depends only on the
    constness of the encapsulating object.

Example:

struct T
{
     private int value;

     property PropType
     {
         @property int getIt() const
         {
             return outer.value;
         }

         alias this = getIt;

         PropType opAssign(int v) const
         {
             outer.value = v;
             return this;
         }

         PropType opAssign(string s : "+")(int v) const
         {
             outer.value += v;
             return this;
         }

         bool isCorrect() const
         {
             return outer.value == 42;
         }
     }

     PropType myProp;
}

Usage:

T t;
int v = t.myProp; // lowers to t.myProp.getIt due to alias this
t.myProp = 41;
t.myProp += 1;
assert(t.myProp.isCorrect());
assert(&t.myProp == null);
assert(T.PropType.sizeof == 0);


More information about the Digitalmars-d mailing list