opApply compilation woes

Gary Willoughby via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 12 10:25:16 PDT 2015


Why does the following code fail to compile if the 
`writeln(value);` line is present?

public template ForeachAggregate(T)
{
	alias ForeachAggregate = int delegate(ref T) nothrow;
}

unittest
{
	import std.stdio;

	class Foo
	{
		private string[] _data = ["foo", "bar", "baz", "qux"];

		public int opApply(ForeachAggregate!(string) dg) nothrow
		{
			int result;

			for (int x = 0; x < this._data.length; x++)
			{
				result = dg(this._data[x]);

				if (result)
				{
					break;
				}
			}

			return result;
		}
	}

	auto foo = new Foo();

	foreach (string value; foo)
	{
		writeln(value);
	}
}


More information about the Digitalmars-d-learn mailing list