www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Standard struct constructors for the heap?

reply "bearophile" <bearophileHUGS lycos.com> writes:
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
May 16 2012
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 struct Node {
     int data;
     Node* next;
 }
 void main() {
     Node n1 = Node(10); // OK
     Node n2 = Node(10, null); // OK
 }
Sorry, I meant: struct Node { int data; Node* next; } void main() { Node n1 = new Node(10); // OK Node n2 = new Node(10, null); // OK } Bye, bearophile
May 16 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 struct Node {
     int data;
     Node* next;
 }
 void main() {
     Node n1 = new Node(10); // OK
     Node n2 = new Node(10, null); // OK
 }
I am sleepy. Third try: struct Node { int data; Node* next; } void main() { Node* n1 = new Node(10); // OK Node* n2 = new Node(10, null); // OK }
May 16 2012
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 5/17/12, bearophile <bearophileHUGS lycos.com> wrote:
 snip
Mixin workaround: import std.conv; property string makeCtors(T)() { T t; string res; foreach (i; 0 .. typeof(t.tupleof).length) { res ~= "this(typeof(this.tupleof[0.." ~ to!string(i+1) ~ "]) tup) { this.tupleof[0.." ~ to!string(i+1) ~ "] = tup; }\n"; } return res; } struct Node { mixin(makeCtors!Node); int data; Node* next; } void main() { Node* n1 = new Node(10); // OK Node* n2 = new Node(10, null); // OK }
May 16 2012
parent "bearophile" <bearophileHUGS lycos.com> writes:
Andrej Mitrovic:

 Mixin workaround:
Yeah, in some cases I have used a similar workaround. Now I'd like to avoid the workaround. Bye, bearophile
May 17 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 16 May 2012 19:50:25 -0400, bearophile <bearophileHUGS lycos.com>  
wrote:

 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
Should be absolutely feasible. I'd also like to see this work: struct X { int x; this(int x) {this.x = x;} } void main(){ X x; // no ctor needed X *xp = new X; // but this is an error! } -Steve
May 17 2012