Bug 4381 – Length attribute for std.typecons.Tuple
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-06-24T04:01:00Z
Last change time
2010-08-14T19:01:55Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2010-06-24T04:01:13Z
This little program (that doesn't work with DMD v2.047) shows why a (constant) length attribute can be quite useful for tuples:
import std.typecons: Tuple;
import std.typetuple: TypeTuple;
template Iota(int start, int stop) { // 2 argument version
static if (stop <= start)
alias TypeTuple!() Iota;
else
alias TypeTuple!(Iota!(start, stop-1), stop-1) Iota;
}
void main() {
alias Tuple!(char, int, int) Unit;
auto u1 = Unit('x', 1, 2);
auto u2 = Unit('y', 10, 20);
foreach (i; Iota!(1, Unit.length))
u1.field[i] += u2.field[i];
}
In that program u1.length (and maybe Unit.length too) needs to be 3.