Question about operations on class/struct properties
Phil Lavoie via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 18 11:07:07 PDT 2014
All you said makes sense. If there is a direct connection between
getter, setter and member than yes, returning it by reference is
usually more convenient:
private T _member;
@property ref inout(T) member() inout {return _member;}
However, sometimes, there is no direct connection between field
and mutators. Sometimes, it is calculated on the fly.
Date {
...
@property auto hour() {return magic / someNumber;}
@property void hour(int newHour) {//complicated algorithm to set
the hour.}
}
Now, assuming we have these two mutators, operators could be
automagically implemented.
Date myDate;
myDate.hour += 4; //myDate.hour(myDate.hour() + 4)
More information about the Digitalmars-d-learn
mailing list