I would say that this advice definitely does not apply to D.
Structures are value types. Classes are reference types. Structures are usually on the stack (although they can be on the heap and indicated by pointers). Classes are on the heap. Structures do not have inheritance or polymorphism. Classes have inheritance and polymorphism.
Structures in D. are very often used. I believe that the general rule is that if something does not need to have polymorphism, it should be a structure. The main reason for choosing a class over a structure is because you want to inherit polymorphism as well.
The second reason may be that if the type has a type of a reference type, it is more appropriate (for example, the container should probably be a class, because copying it every time you pass it to the function will not be good), Structures may have reference semantics but it is just pure to use classes for this.
And finally, if the type contains a lot of data, and you don’t need to have a copy of it every time you pass it to a function, then it would be more efficient to make it a class and only copy it when you really need to (although you can just simply pass it instead of ref).
Structures
D is definitely more interesting than C # structures, since they have destructors and post-dense constructors . There is also no problem with auto-boxing in D, since D uses templates (similar to C ++, although much more powerful and much easier to use), rather than generics. And if you need a pointer to a structure, this is easy to do. So, I really don't think the tip for C # is related to D.
It seems to me that C # tips stem from two problems:
- The fact that structures are types of values and therefore have semantics of values.
- The fact that C # structures must deal with autoboxing and be very concerned about the cost of copying.
Structures are also value types in D, but they are powerful enough to be able to use referential semantics if you want, and adding postblit constructors and destructors makes them much more useful than what you can do with structures in C #. And since D does not have autoboxing, concerns about autoboxing do not apply to D. You still do not want your structures to be huge, because if you do not follow the link, they will be copied whenever you pass them to a function but it is definitely not as important as in C #. This is much more consistent with C ++ in this regard.
And immutability advice doesn't apply at all to D. D structures that change frequently, and that would be a big problem if they weren't. For example, ranges are usually implemented as structures, and this is not possible if you cannot mutate them.
So no, I don’t think the C # advice really applies to D. The situation between the two languages is too different. It would be better to think of D-structures as C ++ - classes that do not have base classes and cannot be obtained, since this is much closer to what they actually are than C # structures.