On Friday, 2 September 2016 at 08:43:45 UTC, dom wrote:
from what i got Classes are always reference types and structs
are value types in D. i am wondering why that is. for me the
main difference between classes and structs is not how they are
allocated, but that one has inhertiance, and the other hasn't.
Supporting inheritance has some overhead, so at least the split
between classes and structs makes sense to me.
How instances in an inheritance tree are allocated is actually an
important consideration, particularly when it comes to object
slicing. In C++, this can be a problem:
```
class Foo {};
class Bar : public Foo {};
Bar bar;
Foo foo = bar;
```
All of the information about the type Bar is lost in the
assignment to foo. The same thing is going to happen when passing
bar to a function that takes a Foo by value as a parameter. The
only way to avoid the problem is to pass by reference (or
pointer). In Modern C++, with move semantics being a thing,
passing by value is much more common than it used to be, but this
is the sort of thing it's easy either not to know or to forget
about. In D, you don't have to worry about it.
I read somewhere (in old forum discussions or an old article)
that object slicing is one of the motivations behind the
distinction in D.