Bug 3514 – opApply should be the first-choice foreach iteration method.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2009-11-16T06:18:00Z
Last change time
2015-06-09T01:27:05Z
Assigned to
nobody
Creator
dsimcha
Comments
Comment #0 by dsimcha — 2009-11-16T06:18:55Z
struct Foo {
uint front() {
return 1;
}
int opApply(int delegate(ref uint) dg) {
return 1;
}
}
void main() {
Foo foo;
foreach(elem; foo) {}
}
test8.d(68): Error: no property 'empty' for type 'Foo'
test8.d(68): Error: no property 'popFront' for type 'Foo'
Clearly, DMD saw that front() was present and tried to use range foreach. This is incorrect because:
1. Only part of the range interface existed. The opApply interface was complete and should have worked.
2. If someone defines both a range interface and an opApply interface with the same types, they probably have a good reason, since ranges serve other purposes, but opApply exists **only** for foreach.
3. Some things, like iterating over trees, can be done more efficiently with control of the stack than without.
Also, once opSlice becomes able to define implicit conversions to ranges for foreach loops, any opApply's defined should take precedence over this for the reasons mentioned above.
Comment #1 by dsimcha — 2009-11-16T06:21:38Z
Forgot to mention: Also, when using virtual functions, it may sometimes be reasonable to define an opApply as an optimization for foreach loops, even if the range-based foreach has the exact same semantics. For example:
class Foo {
SomeType front() { return _front;}
void popFront() {
// do stuff.
}
bool empty() {
return _empty;
}
}
In this case each iteration will require three virtual function calls, whereas if opApply were used, the overhead would be reduced to a single delegate call.
Comment #2 by clugdbug — 2009-11-16T06:40:54Z
Doing this will also fix bug 2984 (which is marked as a regression, but isn't really).
Comment #3 by leandro.lucarella — 2009-11-16T12:44:28Z
*** This issue has been marked as a duplicate of issue 2984 ***
Comment #4 by schveiguy — 2009-11-16T13:07:44Z
This bug supersedes bug 2984, because solving this bug will not only address the problem in 2984, but address a very important problem that is not identified in 2984. That is: if opApply is present *at all* it should override any range functionality when sending to foreach. This includes the case not identified in 2984 in which valid range operations are present *alongside* opApply. e.g.:
import std.stdio;
struct S
{
int front()
{
return 0;
}
bool empty()
{
return true;
}
void popFront()
{
}
int opApply(int delegate(ref int x) dg)
{
writeln("inside opapply");
int x = 0;
return dg(x);
}
}
void main()
{
S s;
foreach(i; s)
{
writeln("inside loop %d", i);
}
}
Currently outputs nothing, it should output:
inside opapply
inside loop 0
Comment #5 by leandro.lucarella — 2009-11-16T13:46:50Z
Then bug 2984 should be marked as a duplicate for this bug, right? There is definitely duplication in this 2 bugs, and there is no point to keep both open.
I won't do it just in case I'm messing it up again ;)
Comment #6 by schveiguy — 2009-11-16T14:01:40Z
Yeah, probably :) I'll do it.
Comment #7 by schveiguy — 2009-11-16T14:04:03Z
*** Issue 2984 has been marked as a duplicate of this issue. ***
Comment #8 by leandro.lucarella — 2009-12-15T16:52:04Z