www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - allowing zero-argument runtime-only struct constructors

Hello everyone!

This is a continuation of a discussion on digitalmars.D.learn.

So I'm investigating the possibility of permitting a syntax which would 
allow a zero-argument struct constructor to be defined and called 
explicitly like their multiple-argument constructor cousins.

On 5/20/11 5:31 PM, Andrej Mitrovic wrote:
 

I see that D's omitting a zero-argument struct constructor has to do with default initialization. What I propose is NOT a "default constructor" that will *automatically* be run any time a struct is declared or implicitly allocated, but rather one that can only be explicitly run at run-time (just like the 1-or-more parameter struct constructors already implemented in D2.0). Would it be possible to implement new semantics so that the following are true: // These two are always default initialized // without constructor call. MyStruct ms1; MyStruct ms2 = MyStruct.init; // Calls zero-argument constructor function, // due to parentheses after type. // Compile-time error if no zero-argument constructor defined. MyStruct ms3 = MyStruct(); // Always default initialized without constructor call. MyStruct* ms5 = new MyStruct; // Calls zero-argument constructor function, // due to parentheses after type. // Compile-time error if no zero-argument constructor defined. // This behavior CANNOT BE GOTTEN with new and opCall in current // versions of D to my knowledge. MyStruct *ms4 = new MyStruct(); Any big reasons why this would be bad, undesirable, or not implementable? The struct's .init property would still be known at compile time so there would be no worries about exceptions being thrown and chaotic stuff happening at compile time. The zero-argument constructor could run at run time or not run according to the choice of the programmer. Also, there would not have to be a distinction between constructor functions of different argument numbers--all constructors could have the same syntax instead of some with regular constructor syntax and another hacked on using opCall. What do people think? -- Christopher
May 20 2011