struct template help

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 12 12:38:22 PDT 2014


On 07/12/2014 12:32 PM, seany wrote:

 > do I have to initialize all variables of the struct?

No. The uninitialized ones get their .init values.

 > or may I also use a
 > this(){} in the struct and initialize only those which are known at a
 > given moment?

That already works with structs. You don't need to define any 
constructor if the type is simple data i.e. if you don't want to set any 
invariants, just construct with whatever is available at that moment:

     auto a = new S("hello");

However, if it is important that someother_var should not remain as an 
empty array, then you better define a constructor and demand both values 
when constructing.

Ali



More information about the Digitalmars-d-learn mailing list