www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Nested Foreach

reply John C <johnch_atms hotmail.com> writes:
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.
Jul 17 2009
next sibling parent Ary Borenszweig <ary esperanto.org.ar> writes:
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. :-)
Jul 17 2009
prev sibling parent reply "Nick Sabalausky" <a a.a> writes:
"John C" <johnch_atms hotmail.com> wrote in message 
news:h3poo6$13j4$1 digitalmars.com...
 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 came across a problem much like that a while back. Does this describe your problem?: http://d.puremagic.com/issues/show_bug.cgi?id=2192 It looks like you're using iterable objects instead of AAs though. If you think it seems like basically the same bug, maybe update the ticket to indicate the problem is more general than just AAs.
Jul 17 2009
parent John C <johnch_atms hotmail.com> writes:
Nick Sabalausky Wrote:

 I came across a problem much like that a while back. Does this describe your 
 problem?:
 http://d.puremagic.com/issues/show_bug.cgi?id=2192
 
 It looks like you're using iterable objects instead of AAs though. If you 
 think it seems like basically the same bug, maybe update the ticket to 
 indicate the problem is more general than just AAs. 
 
 
Looks like it's related, yes. I see that someone else has already noted that the problem happens with opApply, too. Cheers.
Jul 17 2009