我有这个斐波那契数生成器。
struct FibonacciSeries
{
int first = 0;
int second = 1;
enum empty = false;
@property int front() const
{
return first;
}
void popFront()
{
int third = first + second;
first = second;
second = third;
}
@property FibonacciSeries save() const
{
return this;
}
}
这个结构没有take
方法,所以我在执行这个命令时出现这个错误(writeln(FibonacciSeries().take(5))
)。
a.d(66): Error: no property 'take' for type 'FibonacciSeries'
但是,通过导入range
包,它有take
方法。这背后的机制是什么?
最佳答案
机制是统一函数调用语法:
http://dlang.org/function.html#pseudo-member
简单来说,如果 a.foo(b...)
无效,编译器会尝试将其重写为 foo(a, b...)
.
关于import - 如何仅在 D 中导入包来扩展结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29109285/