Compiler doesn't see the inherited opApply in this example

Mike L. sgtmuffles at myrealbox.com
Fri Jul 3 14:42:45 PDT 2009


Hello, can anybody explain why the following code compiles with -version=works but not without?

module test;

import std.stdio;

abstract class Parent
{
	int opApply(int delegate(ref int) dg)
	{
		int fakeDelegate(ref uint fake, ref int content)
			{ return dg(content); }
		return opApply(&fakeDelegate);
	}
	
	abstract int opApply(int delegate(ref uint, ref int) dg);
}

class Child : Parent
{
	override int opApply(int delegate(ref uint, ref int) dg)
	{
		uint index = 0;
		for(int content = 1; content < 6; content++)
		{
			int result = dg(index, content);
			if(result != 0)
				return result;
			index++;
		}
		return 0;
	}
}

void main()
{
	version(works)
		Parent child = new Child();
	else
		Child child = new Child();
	foreach(int content; child)
		writefln(content);
}

I'm making some collection classes and had the same problem. It seems like for some reason the compiler can't match the index-free foreach() in the child, but can in the parent. It obviously knows it's there, or else it wouldn't compile because the index-free opApply would be abstract. Any thoughts?


More information about the Digitalmars-d-learn mailing list