Re: Post-ctor ctor

2011-08-08 Thread Jonathan M Davis
> On 8/9/11 12:25 AM, Jonathan M Davis wrote: > >> On 8/8/11 10:41 PM, Jonathan M Davis wrote: > >>> In this particular example, what goes in initFromParameters is specific > >>> to Foo anyway. So, you'd have to write it by hand regardless. > >> > >> If Andrei intended his answer this way, I don't

Re: Post-ctor ctor

2011-08-08 Thread David Nadlinger
On 8/9/11 12:25 AM, Jonathan M Davis wrote: On 8/8/11 10:41 PM, Jonathan M Davis wrote: In this particular example, what goes in initFromParameters is specific to Foo anyway. So, you'd have to write it by hand regardless. If Andrei intended his answer this way, I don't quite see how it would b

Re: Post-ctor ctor

2011-08-08 Thread Andrej Mitrovic
On 8/8/11, KennyTM~ wrote: > Template-mixin is often shorter and less error-prone. > - > > mixin template FieldInit(size_t count, alias fun) > { > this(RepresentationTypeTuple!(typeof(this))[0..count] params) > { > foreach (y, x; __traits(allMembers, typeof(this)

Re: Post-ctor ctor

2011-08-08 Thread Jonathan M Davis
> On 8/8/11 10:41 PM, Jonathan M Davis wrote: > > In this particular example, what goes in initFromParameters is specific > > to Foo anyway. So, you'd have to write it by hand regardless. > > If Andrei intended his answer this way, I don't quite see how it would > be an appropriate solution, as An

Re: Post-ctor ctor

2011-08-08 Thread KennyTM~
On Aug 9, 11 05:05, Andrej Mitrovic wrote: Done deal: import std.traits; string FieldInit(T, int len, string fun)() { string result; auto fields = [__traits(allMembers, T)]; result ~= "this("; foreach (y, x; (RepresentationTypeTuple!T)[0..len]) { result ~= x.

Re: Post-ctor ctor

2011-08-08 Thread Andrej Mitrovic
Done deal: import std.traits; string FieldInit(T, int len, string fun)() { string result; auto fields = [__traits(allMembers, T)]; result ~= "this("; foreach (y, x; (RepresentationTypeTuple!T)[0..len]) { result ~= x.stringof ~ " " ~ fields[y] ~ ", "; } resul

Re: Post-ctor ctor

2011-08-08 Thread David Nadlinger
On 8/8/11 10:41 PM, Jonathan M Davis wrote: In this particular example, what goes in initFromParameters is specific to Foo anyway. So, you'd have to write it by hand regardless. If Andrei intended his answer this way, I don't quite see how it would be an appropriate solution, as Andrej explici

Re: Post-ctor ctor

2011-08-08 Thread Jonathan M Davis
> On 8/8/11 6:08 AM, Andrei Alexandrescu wrote: > > On 8/7/11 1:16 PM, Andrej Mitrovic wrote: > >> struct Foo > >> { > >> int a; > >> int b; > >> int c; > >> int d; > >> } > > > > Just define a mixin and use it: > > > > this(int a, int b, int c, int d) { > > mixin(initFromParameters()); > > ... >

Re: Post-ctor ctor

2011-08-08 Thread Timon Gehr
David Nadlinger wrote: > On 8/8/11 6:08 AM, Andrei Alexandrescu wrote: >> On 8/7/11 1:16 PM, Andrej Mitrovic wrote: >>> struct Foo >>> { >>> int a; >>> int b; >>> int c; >>> int d; >>> } >> Just define a mixin and use it: >> >> this(int a, int b, int c, int d) { >> mixin(initFromParameters()); >> .

Re: Post-ctor ctor

2011-08-08 Thread David Nadlinger
On 8/8/11 6:08 AM, Andrei Alexandrescu wrote: On 8/7/11 1:16 PM, Andrej Mitrovic wrote: struct Foo { int a; int b; int c; int d; } Just define a mixin and use it: this(int a, int b, int c, int d) { mixin(initFromParameters()); ... } There is currently no »legal« way to get the parameter name

Re: Post-ctor ctor

2011-08-08 Thread Andrei Alexandrescu
On 8/8/11 1:59 PM, bearophile wrote: Andrei Alexandrescu: this(int a, int b, int c, int d) { mixin(initFromParameters()); ... } A similar macro could help assignments. Do you see people using that in real code? Yes. Andrei

Re: Post-ctor ctor

2011-08-08 Thread bearophile
Andrei Alexandrescu: > this(int a, int b, int c, int d) { >mixin(initFromParameters()); >... > } > > A similar macro could help assignments. Do you see people using that in real code? Bye, bearophile

Re: Post-ctor ctor

2011-08-07 Thread Andrei Alexandrescu
On 8/7/11 1:16 PM, Andrej Mitrovic wrote: Just throwing an idea.. structs are great since you don't have to make a special constructor just to initialize its fields. E.g.: struct Foo { int a; int b; int c; int d; } void main() { auto foo = Foo(1, 2, 3, 4); } But what

Re: Post-ctor ctor

2011-08-07 Thread bearophile
Trass3r: > > this(int this.a, int this.b, int this.c, int this.d) { > > sum = a + b + c + d; > > average = (a + b + c + d) / 4; > > } > > Can't express how awkward this is. Why don't you try to express how awkward it is? Well, code like this is shorter than what the OP h

Re: Post-ctor ctor

2011-08-07 Thread Trass3r
this(int this.a, int this.b, int this.c, int this.d) { sum = a + b + c + d; average = (a + b + c + d) / 4; } Can't express how awkward this is.

Re: Post-ctor ctor

2011-08-07 Thread bearophile
> With that idea your struct becomes: Or just: struct Foo { int a, b, c, d, sum, average; this(this.a, this.b, this.c, this.d) { sum = a + b + c + d; average = (a + b + c + d) / 4; } } Bye, bearophile

Re: Post-ctor ctor

2011-08-07 Thread bearophile
Andrej Mitrovic: > Good idea / bad idea? I prefer a solution that to me looks simpler, discussed a bit here: http://d.puremagic.com/issues/show_bug.cgi?id=3878 With that idea your struct becomes: struct Foo { int a, b, c, d, sum, average; this(int this.a, int this.b, int this.c, int th

Post-ctor ctor

2011-08-07 Thread Andrej Mitrovic
ve to be initialized based on only the first 4 fields, and I still want to keep the same call in the user code? Here's a hypothetical case: struct Foo { int a; int b; int c; int d; int sum; int average; post this() // post-ctor ctor { sum = a +