Static foreach.
Kirk McDonald
kirklin.mcdonald at gmail.com
Sat Nov 4 02:27:50 PST 2006
Walter Bright wrote:
> Nick wrote:
>> Here's a useful example (I think):
>>
>> void rotLeft(X...)()
>> {
>> typeof(X[0]) tmp = X[0];
>> foreach(int i, x; X[1..$])
>> X[i] = x;
>> X[$-1] = tmp;
>> }
>>
>> void main()
>> {
>> int a=1, b=2, c=3;
>> rotLeft!(a,b,c); // Should produce a,b,c = 2,3,1
>> }
>>
>> Compiling gives the error "Integer constant expression expected
>> instead of i".
>>
>> I'm using this right now, but instead of being generic, the version I
>> have
>> contains lots statements like
>> ...
>> static if(X.length > 2) X[1] = X[2];
>> static if(X.length > 3) X[2] = X[3];
>> etc.
>
> Template tuple parameters aren't function parameters that can be
> modified. You'll need to pass a, b, c as inout parameters to the
> function, not the template.
Which once again brings up a problem. This doesn't work:
void func(T ...)(inout T t) {
// ...
}
Similarly, this doesn't work:
void func(T ...)(T t) {
foreach(inout arg; t) {
// do something with args
}
// call with modified args
other_func(t);
}
That idiom would be /so useful/.
--
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
More information about the Digitalmars-d
mailing list