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 = 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/

相关文章:

build-automation - D的实际建筑系统

python - python-igraph 错误 'module' 对象没有属性 'Graph'

php - 使用相同库的多个文件的Python导入技术

r - Excel 时间并不总是正确转换

python - 从一个子模块导入到另一个子模块

d - 遍历 D 中文件夹中的文件

css - 即使来源正确,@font-face 也不起作用

sockets - 允许 D 应用程序和浏览器之间双向通信的工具链

alias - 如何将 std.typecons.Typedef 与函数一起使用?

d - 用 LDC2 和 GDC 编译