Getting consistent behavour for class properties
mikey via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Oct 2 10:10:58 PDT 2016
On Sunday, 2 October 2016 at 14:44:13 UTC, Lodovico Giaretta
wrote:
> Yeah, a property is quite different from a variable. In fact, a
> property may refer to a computed value, which may not have an
> address and as such cannot be modified:
> [...]
> So it is correct that `+=` doesn't work with properties, and
> neither does the "addressof" operator `&` (it returns the
> delegate). Having a getter property return by ref circumvents
> this issue, but has the drawback of bypassing any check that a
> setter property would do.
There is already a degree of transparency with how properties
being handled for example in allowing properties to be an lvalue
if they have a setter.
t.val = 42;
I can understand that on an implementation level it might be
simpler to interpret the 2 statements differently but it would
seem much more intuitive to me if the syntactic sugar for
properties were extended out to allow statements such as:
t.val += 1;
Where there is a getter and setter available.
To my mind at least the ideal would be for properties to act as
close as possible to member variables.
It is however good to see that it's possible to wrap functions to
provide some of this behaviour.
More information about the Digitalmars-d-learn
mailing list