On Wednesday, 15 August 2012 at 00:32:43 UTC, Walter Bright wrote:
On 8/14/2012 3:57 PM, Mehrdad wrote:
I guess they aren't really default constructors, then <g>.

I say potayto, you say potahto...  :P


So what happens when you allocate an array of them?

For arrays, they're called automatically.


Well, OK, that's a bit of a simplification.

It's what happens from the user perspective, not the compiler's (or runtime's).

Here's the full story.
And please read it carefully, since I'm __not__ saying D should adopt what C# does word for word!

In C#:
- You can define a custom default constructor for classes, but not structs. - Structs _always_ have a zero-initializing default (no-parameter) constructor. - Therefore, there is no such thing as "copy construction"; it's bitwise-copied. - Ctors for _structs_ MUST initialize every field (or call the default ctor)
- Ctors for _classes_ don't have this restriction.
- Since initialization is "Cheap", the runtime _always_ does it, for _security_.
- The above^ is IRRELEVANT to the compiler!
  * It enforces initialization where it can.
* It explicitly tells the runtime to auto-initialize when it can't. -- You can ONLY take the address of a variable in unsafe{} blocks. -- This implies you know what you're doing, so it's not a problem.


What D would do _ideally_, IMO:

1. Keep the ability to define default (no-args) and postblit constructors.

2. _Always_ force the programmer to initialize _all_ variables explicitly.
   * No, this is NOT what C++ does.
* Yes, it is tested & DOES work well in practice. But NOT in the C++ mindset. * If the programmer _needs_ vars to be uninitialized, he can say = void. * If the programmer wants NaNs, he can just say = T.init. Bingo.


It should work pretty darn well, if you actually give it a try.

(Don't believe me? Put it behind a compiler switch, and see how many people start using it, and how many of them [don't] complain about it!)


D could take a similar approach.

It could, but default construction is better (!).

Well, that's so convincing, I'm left speechless!

Reply via email to