Standard struct constructors for the heap?

bearophile bearophileHUGS at lycos.com
Wed May 16 16:50:25 PDT 2012


Regarding the efforts of removing limitations from D, do you know 
if there are problems in implementing this oldish enhancement 
request?

http://d.puremagic.com/issues/show_bug.cgi?id=4086

The idea is to just allow the heap creation of simple structs 
with no need to define a constructor:


struct Node {
     int data;
     Node* next;
}
void main() {
     Node n1 = Node(10); // OK
     Node n2 = Node(10, null); // OK
}


I am writing many of those stupid struct initializations, it's 
boring busy work and they don't make the code more readable, just 
longer, so I'd like D to define them by itself:

this(int data_=int.init, Node* next_=(Node*).init)
pure nothrow @safe {
     this.data = data_;
     this.next = next_;
}

Removing this limit also makes D more uniform with 
locally-allocated struct construction semantics.

Bye,
bearophile


More information about the Digitalmars-d mailing list