【问题标题】:How to pass file.ByChunk in dlang?如何传递文件。通过俚语中的块?
【发布时间】:2017-06-02 17:22:00
【问题描述】:

我是 D 的新手。

我有以下代码:

auto file = File("test.txt", "r");
scope(exit) file.close();

foreach (letter; getTextKernel(file.byChunk(8192))) {
    writeln(letter);
}

我的 getTextKernel 看起来像:

string[] getTextKernel(InputRange!ubyte[] text) pure { ... }

我收到错误: getTextKernel (InputRange!ubyte[] text) 不能使用参数类型 (ByChunk) 调用

关于 byChunk 的文档:Returns an input range set up to read from the file handle a chunk at a time.

更新:整个计划

import std.stdio;

string[] getTextKernel(R)(R text) pure {
    return ["aa", "bbb", "ccc"];
}

void main() {
    writeln("Hello master dmitry!");

    auto file = File("test.txt", "r");
    scope(exit) file.close();

    // StubdGenetics genetic;
    foreach (letter; getTextKernel(file.byChunk(8192))) {
        writeln(letter);
    }
}

错误:纯函数 'app.getTextKernel!(ByChunk).getTextKernel' 不能调用不纯的析构函数 'std.stdio.File.ByChunk.~this'

【问题讨论】:

    标签: d dmd


    【解决方案1】:

    通常的方法是让getTextKernel 接受模板参数。

    string[] getTextKernel(R)(R text) pure   { ... }
    

    因为范围在 D 中是惰性的,所以它们通常是自定义的一次性类型。 InputRange 在我的经验中几乎从未使用过。使用它是有原因的,但它速度较慢(使用运行时调度而不是编译时间)并且通常不需要。

    【讨论】:

    • 请注意,您可能希望用isInputRange!Ris(ElementType!R : ubyte[]) 约束R(两者都由std.range 提供)
    • 谢谢,但现在我遇到了pure 修饰符的问题。查看我的更新
    • bychunk 的东西不是纯的。除了不使用纯或传递块本身而不是范围之外,您对此无能为力
    猜你喜欢
    • 2014-11-06
    • 2014-11-04
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2015-10-05
    • 2017-03-20
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多