首页 文章

如何声明变量File.byLine()被赋值给?

提问于
浏览
3

我需要一个看起来类似的类或结构

struct ThingReader {
    ???? lines;
    Thing thing;

    this(File f) {
        this.lines = f.byLine;
        popFront;
    }

    @property bool empty()      { return lines.empty; }
    @property ref Thing front() { return thing; }
    void popFront() {
        if (! empty) {
            auto l = lines.front;
            lines.popFront;
            parseLine(l, thing); // Not shown
        }
    }
}

但我不知道什么类型的声明放在哪里????是 .

如果我尝试 auto lines ,那么错误是"Error: no identifier for declarator lines" .

如果我将类型推断留给编译器并尝试类似:

struct ThingReader(Lines) {
    Lines lines;
    Thing thing;

    this(File f) {
        this.lines = f.byLine;
        popFront;
    }
    // etc.
}

然后编译器似乎没有这个声明,但是当我稍后尝试声明 auto reader = ThingReader(f) 时,我得到"Error: struct huh.ThingReader cannot deduce function from argument types !()(File)" .

声明File.byLine函数返回 auto 但是(见上文)对我不起作用 .

当我声明 auto lines = f.byLine 并检查其类型时,我可以看到它是 ByLine!(char, char) .
当我尝试声明 ByLine lines 时,我得到了"Error: undefined identifier ByLine",当我尝试声明 std.stdio.ByLine lines 时,我得到"Error: undefined identifier ByLine in module std.stdio" .
当我尝试声明一个 ByLine!(char, char) 时,我得到"Error: template instance ByLine!(char, char) template 'ByLine' is not defined", std.stdio.ByLine!(char, char) 给了我"Error: template identifier 'ByLine' is not a member of module 'std.stdio'" .

1 回答

  • 3

    正如Adam在评论中所提到的,您可以使用 typeof(File.byLine()) 来推断出您想要的类型;有必要添加最后的括号,这就是 typeof(File.byLine) 不起作用的原因 . 您无法明确指定 lines 类型的原因是因为 byLine 函数返回的结构是私有的,因此无法从 std.stdio 模块外部引用 .

相关问题