John C escribió:
Is nesting foreach statements supposed to work? I hit a problem today that I've
never encountered before, even though I'm sure I've nested foreach before.
Here's an example that illustrates the problem:
class Collection {
int opApply(int delegate(ref Object) a) {
return 0;
}
}
Object testForeach(Collection level1, Collection level2) {
foreach (first; level1) {
foreach (second; level2) {
return second;
}
}
return null;
}
void main() {
testForeach(new Collection, new Collection);
}
Error: cannot implicitly convert expression (second) of type object.Object to
int
If someone else can repro this, I'll enter it into Bugzilla.
John.
I can confirm that it's a bug. And I can see in Descent the testForeach
function is translated to this:
Object testForeach(Collection level1, Collection level2) {
switch(level1.opApply(delegate (Object __applyArg0) {
{
Object first = __applyArg0;
switch(level2.opApply(delegate (Object __applyArg0) {
{
Object second = __applyArg0;
{
// ** Here's the problem **
__result = cast(Object) cast(int) second;
return 2;
}
}
return 0;
} )) {
default:
break;
case 2:
__result = __result;
return 2;
}
}
return 0;
} )) {
default:
break;
case 2:
return __result;
}
return cast(Object) null;
}
Maybe that helps fixing it. :-)