Are D classes proper reference types?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sun Jun 27 09:55:45 UTC 2021


On Sunday, 27 June 2021 at 09:35:10 UTC, IGotD- wrote:
> Probably about all managed languages.

I am sceptical of this assumption. There are no reasons for a GC 
language to require the usage of fat pointers?

> When you use a struct as a member variable in another struct 
> the data will be expanded into the host struct. If the member 
> struct is 16 bytes then the host struct will have grow 16 bytes 
> to accommodate that member struct.
>
> This is not the case in D with classes as classes always are 
> allocated on the heap using dynamic allocation. This leads to 
> more fragmentation and memory consumption.

Ok, I understand what you mean, but classes tend to be used for 
"big objects". I don't think there is anything that prevents a 
private class reference to be replaced by an inline 
representation as an optimization if no references ever leak. If 
you use whole program optimizations such things could also be 
done for public members.

What is holdning D back here is the lack of a high level IR after 
the frontend where such global passes could improve the 
implementation quality.

So, this is at the core of good language design; keep the model 
simple, but enable and allow optimizations. Too much special 
casing and you end up with a language that is difficult to extend 
and a neverending discovery of corner cases and compiler bugs.



More information about the Digitalmars-d-learn mailing list