Re: Standard struct constructors for the heap?

2012-05-17 Thread bearophile

Andrej Mitrovic:


Mixin workaround:


Yeah, in some cases I have used a similar workaround. Now I'd 
like to avoid the workaround.


Bye,
bearophile


Re: Standard struct constructors for the heap?

2012-05-17 Thread Steven Schveighoffer
On Wed, 16 May 2012 19:50:25 -0400, bearophile bearophileh...@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


Standard struct constructors for the heap?

2012-05-16 Thread bearophile
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


Re: Standard struct constructors for the heap?

2012-05-16 Thread bearophile

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


Re: Standard struct constructors for the heap?

2012-05-16 Thread bearophile

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
}


Re: Standard struct constructors for the heap?

2012-05-16 Thread Andrej Mitrovic
On 5/17/12, bearophile bearophileh...@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
}