Future of memory management in D

Dom DiSc dominikus at scherkl.de
Fri Nov 19 11:14:46 UTC 2021


On Friday, 19 November 2021 at 10:21:35 UTC, Max Samukha wrote:
> On Thursday, 18 November 2021 at 19:32:36 UTC, Atila Neves 
> wrote:
>
>>
>> Works with `ref prop() { return _fld; }`.
>
> The whole point of properties is to hide private state. 
> Exposing a private member for direct mutation is generally a 
> bad idea.

Exactly.
What I would expect from a "real" property is that

prop += 5;

is automatically lowered to

prop(prop() + 5); // function style

or direct

auto tmp = prop; tmp += 5; prop = tmp;

or whatever - but would work only if there is both a getter _and_ 
a setter.
And if only one of the two is implemented, a fitting error 
message should be emitted like
"no setter for prop available" or "no getter for prop available".

It should NEVER EVER set a property through the getter!!!
ref prop() { ... } is a very bad anti-pattern.
If "const" is missing or a reference is returned from a getter (a 
@property function without parameter) this should be a compile 
error!
Unless that is given I don't consider @property to be a 
ready-to-use feature.


More information about the Digitalmars-d mailing list