Restrict Class Properties?
Michiel
nomail at please.com
Sat Feb 24 01:50:31 PST 2007
Miles wrote:
>> But ++, -- and op= have their own definitions with certain class-types
>> that can't be defined in terms of only setter and getter functions.
>
> For composite types, you implement properties with a validator and a
> refresher, along with the setter and the getter.
>
> The validator is called before changing the object, with a copy of the
> object with its new instance as a parameter. It either returns or throw
> an exception.
>
> The refresher is called after changing the object, to do whatever would
> have been done if the setter was called to change the object.
>
> A property += 5 translates more or less like this:
>
> {
> auto tmp = property.value.dup;
> tmp.opAddAssign(5);
> property.validate(tmp); // may throw
> property.refresh();
> }
>
> Of course, the compiler is responsible to optimize this. If the property
> doesn't implement a validator, there is no need to dup.
That's a good idea. Better than declaring each operator again. Though
less efficient, I suppose, if you do want to validate.
--
Michiel
More information about the Digitalmars-d
mailing list