Another Properties Proposal

Chris Nicholson-Sauls ibisbasenji at gmail.com
Wed Aug 22 15:10:36 PDT 2007


Chad J wrote:
> Another possible syntax (without new keywords, almost looks better too):
> in Type foo(getFoo); // RO property
> out Type foo(setFoo); // WO property
> inout Type foo(getFoo,setFoo); // RW property
> 
> So far this is pretty much what Ender KaShae suggested in the original 
> thread.

I like the idea of re-using in/out/inout, but I think a new 'property' keyword and a pair 
of braces are also a good idea.  Properties really ought to be whole entities 
(encapsulation), or at least it seems so to me -- and then maybe we could re-use the 
precedent established with templates (that of a member with the same name as the 
template).  Then use operator overloads to do the rest.

property ulong area {

   ulong area () { return width * height; }

   ulong opAssign (ulong x) {
     ulong xx = x / 2_UL ;

     width = xx;
     height = x - xx;
     return area;
   }

   ulong opAssign (char[] x) { area = toULong(x); }

}


Etc.  This could be treated internally by D as just a struct which calls the self-named 
function (which it would be an error to omit) when there is no appropriate operator 
overload to use.

Hmm.  If the self-named function is required, maybe the syntax could account for it:

class Rect {

   ulong width, height ;

   property ulong area {
     return width * height;
   }{
     void opAssign (ulong x) {
       ulong xx = x / 2_UL ;

       width = xx;
       height = x - xx;
     }

     void opAssign (char[] x) { area = toULong(x); }
   }

}

Okay, so a double pair of braces is a bit odd... so are the double parentheses used in 
function templates.  :)  Speaking of templates...

template MMult2Prop (alias A, alias B) {
   property typeof(A) MMult2Prop {
     return A * B;
   }{
     void opAssign (typeof(A) x) {
       typeof(A) xx = x / 2;

       A = xx;
       B = x - xx;
     }
   }
}

class Rect {

   ulong width, height ;

   mixin MMult2Prop!(width, height) area ;

}

I haven't really been following the latest property discussion too close, so forgive me if 
something similar has come up already.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list