如何使一个类在foreach语句中可用?
该类包含一个关联数组(例如string[string])。因此,foreach语句使用此数组作为源。
所以这就是我想要的:
auto obj = new Obj();
foreach (key, value; obj)
{
...
}
我需要实现类似这样的接口吗?
编辑:
解决方案:
public int opApply(int delegate(ref string, ref Type) dg)
{
int result = 0;
foreach (ref key, ref value; data)
{
result = dg(key, value);
if (result != 0)
{
break;
}
}
return result;
}
同样的操作也适用于public int opApply(int delegate(ref Type) dg)。
opApply
在D2中也可以使用。 - kennytm