Comment #0 by bearophile_hugs — 2013-01-11T10:47:04Z
Since some time the D foreach is able to unpack tuples, as visible in the main() regarding Foo1:
import std.typecons: Tuple;
alias Two = Tuple!(int, int);
struct Foo1 {
bool empty = false;
@property Two front() { return Two(1, 2); }
void popFront() { empty = true; }
}
struct Foo2 {
int opApply(int delegate(ref Two) dg) {
int result;
auto aux = Two(1, 2);
result = dg(aux);
return result;
}
}
void main() {
foreach (x_y; Foo1()) {} // OK
foreach (x, y; Foo1()) {} // OK
foreach (x_y; Foo2()) {} // OK
foreach (x, y; Foo2()) {} // line 21, error.
}
But currently in dmd v.2.062alpha the same doesn't happen for a tuple given by opApply:
test.d(21): Error: cannot uniquely infer foreach argument types
Maybe it's better for the last foreach to be valid code.
Comment #1 by bearophile_hugs — 2014-10-11T11:19:51Z
This is a bad idea, it's better to add a general and safe syntax to unpack tuples in foreach. Closed.