Future of memory management in D
Rumbu
rumbu at rumbu.ro
Sat Nov 20 13:32:40 UTC 2021
On Saturday, 20 November 2021 at 12:11:14 UTC, Commander Zot
wrote:
> On Saturday, 20 November 2021 at 11:53:20 UTC, Rumbu wrote:
>> The original issue was the how can I design a property in D so
>> I can write:
>>
>> ```
>> area +=1;
>> ```
>
> import std.stdio;
>
> struct Property(T) {
> T* t;
>
> T opOpAssign(string op)(T r)
> {
> mixin("(*t) "~op~"= r;");
> return *t;
> }
> }
>
> class Square
> {
> private double width = 42;
> public auto area()
> {
> return Property!double(&width);
> }
> }
>
> void main() {
> auto s = new Square;
> writeln(s.width);
> s.area += 1;
> writeln(s.width);
> }
Try again. Width must be 42.01 after you increase area by 1, not
43.
And again, it's not about that you can find or not an workaround,
it's about the fact that @property is half baked in the language.
More information about the Digitalmars-d
mailing list