Bug 11279 – Error: no [] operator overload for type Tuple!(int, int, int)
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-15T22:03:00Z
Last change time
2013-10-16T00:20:53Z
Assigned to
nobody
Creator
thelastmammoth
Comments
Comment #0 by thelastmammoth — 2013-10-15T22:03:39Z
void main(){
import std.typecons;
auto a=tuple(0,1,2);
a[0]=0;//OK
foreach(i;0..3)
a[i]=0;//Error: no [] operator overload for type Tuple!(int, int, int)}
}
Comment #1 by monarchdodra — 2013-10-16T00:20:53Z
for typetuple, operator[] is a "static" operator: The index *must* be known at compile time, as the return type depends on the parameter. It's actually different overloads for each different index:
EG:
auto a = tuple('a', 5, 2.2);
char c = a[0]; //Calls the first arg
int i = a[1]; //Calls the second arg
double d = a[2]; //Calls the last arg
Because of this putting an operator[] inside a for loop is not possible, as you are basically trying to solve overloads dynamically, which is not possible in a static language like D.
What could be a workaround for you is the static foreach construct:
void main(){
import std.typecons, std.typetuple;
auto a=tuple(0,1,2);
a[0]=0;//OK
foreach(I;TypeTuple!(0, 1, 2))
a[I]=0;//Error: no [] operator overload for type Tuple!(int, int, int)}
}
What this does is generate a "type sequence", which contains the "Types" (or in this case, value parameters) 1, 2 and 3.
Now, the foreach is statically iterating over the parameters, generating a *unique* body for each loop index. Notice that this time, I used the name "I" this denotes that this argument is actually statically known inside the body of the loop.
Closing as invalid.