www.digitalmars.com         C & C++   DMDScript  

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

reply Christopher the Magnificent <ultimatemacfanatic gmail.com> writes:
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
parent kenji hara <k.hara.pg gmail.com> writes:
Some days ago, I created patch, and posted pull request #72.
http://d.puremagic.com/issues/show_bug.cgi?id=3D6036
https://github.com/D-Programming-Language/dmd/pull/72

This patch fixes some opCall problem, and allow a zero-argument
default constructor on stack allocation, even exists multiple-argument
explicit constructors.

 =A0 =A0 =A0 =A0// Calls zero-argument constructor function,
 =A0 =A0 =A0 =A0// due to parentheses after type.
 =A0 =A0 =A0 =A0// Compile-time error if no zero-argument constructor defi=

 =A0 =A0 =A0 =A0MyStruct ms3 =3D MyStruct();

static opCall is defined).
 =A0 =A0 =A0 =A0// Calls zero-argument constructor function,
 =A0 =A0 =A0 =A0// due to parentheses after type.
 =A0 =A0 =A0 =A0// Compile-time error if no zero-argument constructor defi=

 =A0 =A0 =A0 =A0// This behavior CANNOT BE GOTTEN with new and opCall in c=

 =A0 =A0 =A0 =A0// versions of D to my knowledge.
 =A0 =A0 =A0 =A0MyStruct *ms4 =3D new MyStruct();

Please post a new report into bugzilla with test cases. Kenji Hara
May 21 2011