On Tuesday, 17 May 2022 at 14:40:48 UTC, Kevin Bailey wrote:
Foo foo;

is undefined behavior waiting to happen, that I can't detect at a glance.

It is actually perfectly well defined - for the class, it will be null, and this will kill the program if you use it.

You might not like that definition, but that's how it is defined.

- A *secondary* goal would be for class objects to be able to have deterministic destructors like structs.

they can with the `scope` keyword.

The first looks trivial: Just have it allocate a Foo by default.

Worth noting that D *never* calls a user defined function on variable declaration; there are no default constructors.

Even if it is a struct, it never actually calls a constructor, it just copies over the default init value.

This is different than classes, which have some kind of constructor call any time you make one.


I think it is probably this default constructor stance that the rest flows from: a class is assumed to encapsulate some non-trivial state so it has a constructor, interfaces, object identities, etc. A struct is more of a plain collection of data.

Of course, in practice the lines are more blurred than that, but I think that's where it comes from. Especially if you look at the older D versions when structs didn't support constructors at all.

Reply via email to