Getting consistent behavour for class properties
Lodovico Giaretta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Oct 2 07:44:13 PDT 2016
On Sunday, 2 October 2016 at 14:26:46 UTC, mikey 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:
@property auto currentTimeMillis()
{
return currentTimeNanos/1000000;
}
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.
If you want to use properties as getters/setters and also want
compound assignments to work, you can easily achieve that with a
wrapper template as this[1] one.
[1] http://pastebin.com/38n0fEtF
More information about the Digitalmars-d-learn
mailing list