> 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
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
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)
> 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
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.
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
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
> 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());
> > ...
>
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());
>> .
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
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
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
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
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
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.
> 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
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
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 +
18 matches
Mail list logo