Future of memory management in D

rumbu rumbu at rumbu.ro
Fri Nov 19 16:31:19 UTC 2021


On Friday, 19 November 2021 at 16:14:41 UTC, Alexandru Ermicioi 
wrote:
> On Friday, 19 November 2021 at 15:39:54 UTC, 12345swordy wrote:
>>
>> It violate encapsulation. Which at that point you might as 
>> well make the l-value public.
>>
>> -Alex
>
> It doesn't. Just like any other setter in any other language, 
> it does not constraint you to return a private field in the 
> object itself. You can return any value from any source, be it 
> in object itself, a sub-object, on heap or on stack. The only 
> constraint is to have it stored somewhere, so you can return a 
> reference.
>
> The main problem here, is that people expect for a value type 
> to behave like a reference type here, which isn't the case in 
> any other language too. Just try for example to return an int 
> from a getter in java, and do ++ on it. You'll get the same 
> behavior as in D.
>
> Best regards,
> Alexandru.

```d
class Square
{
     private double width;
     public double area()
     {
         return width * width;
     }
     public ref float area(double x)
     {
        width = sqrt(x);
       //what should I return here as ref?
     }
}
```

I don't know in Java, but in C# this works:


```csharp

class Square
{
     private double width;
     public double area
     {
         get => width * width;
         set => width = Sqrt(value);
     }
     Square sq = new Square();
     sq.area += 1;
}
```





More information about the Digitalmars-d mailing list