Future of memory management in D
Commander Zot
no at no.no
Sat Nov 20 14:33:53 UTC 2021
On Saturday, 20 November 2021 at 13:32:40 UTC, Rumbu wrote:
> 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.
this was just an example how you can actually implement
properties that support whatever operation you want. @property
and the assignment rewrite should just be removed from the
language in my opinion.
More information about the Digitalmars-d
mailing list