Import 仅在D中导入包如何扩展结构?
我有一个斐波那契数发生器Import 仅在D中导入包如何扩展结构?,import,d,Import,D,我有一个斐波那契数发生器 struct FibonacciSeries { int first = 0; int second = 1; enum empty = false; @property int front() const { return first; } void popFront() { int third = first + second; first = se
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))
)
但是,通过导入
range
包,它具有take
方法。这背后的机制是什么 机制是统一的函数调用语法:
简单地说,如果a.foo(b…)
无效,编译器将尝试将其重写为foo(a,b…)
a.d(66): Error: no property 'take' for type 'FibonacciSeries'