我有这个斐波那契数字生成器.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这个结构没有这个take
方法,所以我在执行这个命令时遇到这个错误(writeln(FibonacciSeries().take(5))
).
a.d(66): Error: no property 'take' for type 'FibonacciSeries'
Run Code Online (Sandbox Code Playgroud)
但是,通过导入range
包,它有take
方法.这背后的机制是什么?
机制是统一函数调用语法:
http://dlang.org/function.html#pseudo-member
简单地说,如果a.foo(b...)
无效,编译器会尝试将其重写为foo(a, b...)
.