如何在 D 中使对象(类)可访问?
如何使一个类可在 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) 也是如此。
how can I make a class usable in a foreach statement?
The class contains a associative array (e.g. string[string]). So the foreach statement use this array as source.
So this is what I want:
auto obj = new Obj();
foreach (key, value; obj)
{
...
}
Do I need to implement a interface someting like that?
EDIT:
The solution:
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;
}
Same is done for public int opApply(int delegate(ref Type) dg).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
D1:
D2:
D1:
D2:
OP 发布的解决方案是一个有效的解决方案,但在 D2 中还有另一个具有一组不同权衡的解决方案。 D 中的迭代可以分为内部迭代(由 opApply 处理)和外部迭代(由范围处理)。
内部迭代赋予正在迭代的对象对调用堆栈的控制。例如,这允许被迭代的对象使用递归,而无需维护显式堆栈,但使得以锁步方式迭代多个结构变得不可能。外部迭代则相反。
外部迭代是通过范围完成的。范围是定义三个方法的任何类或结构:
front()
提供对范围中第一个元素的访问,popFront()
前进范围和empty如果范围为空,()
返回true
。如果范围是无限的,则可以将empty声明为常量而不是成员函数。通过这种方式,调用者可以控制调用堆栈,这是一个根据具体情况可能好也可能坏的权衡。以下是使用范围进行迭代的示例:
The solution posted by the OP is a valid solution, but in D2 there's another one with a different set of tradeoffs. Iteration in D can be divided into internal iteration, which is handled by opApply, and external iteration, which is handled by ranges.
Internal iteration gives the object being iterated over control of the call stack. This allows, for example, recursion to be used by the object being iterated over without maintaining an explicit stack, but makes iterating over multiple structures in lockstep impossible. External iteration does the opposite.
External iteration is accomplished via ranges. A range is any class or struct that defines three methods:
front()
gives access to the first element in the range,popFront()
advances the range andempty()
returnstrue
if the range is empty. If the range is infinite, empty can be declared as a constant instead of a member function. In this way, the caller has control over the call stack, which is a tradeoff that can be good or bad depending on the situation.Here's an example of using ranges for iteration: