【发布时间】:2014-06-08 19:13:51
【问题描述】:
我有以下代码块,可以在 DMD v2.063.2 上干净地编译
import std.stdio;
import std.string;
import std.file;
void main(string[] args)
{
auto file = File("a_file.txt", "rb");
string line;
string source;
while ((line = file.readln()) !is null)
{
source.append(line);
}
writeln("--- source: ---");
writeln(source);
writeln("---------------");
}
但是,每次运行时,都会发生这个错误:
std.file.FileException@std/file.d(386): : No such file or directory
----------------
5 test 0x000000010b955c9e void std.file.writeImpl(const(char[]), const(void[]), const(uint)) + 142
6 test 0x000000010b955c08 void std.file.append(const(char[]), const(void[])) + 56
7 test 0x000000010b92787e _Dmain + 174
8 test 0x000000010b94330d extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).void runMain() + 33
9 test 0x000000010b942e59 extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).void tryExec(scope void delegate()) + 45
10 test 0x000000010b943359 extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).void runAll() + 61
11 test 0x000000010b942e59 extern (C) int rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).void tryExec(scope void delegate()) + 45
12 test 0x000000010b942e0d _d_run_main + 457
13 test 0x000000010b942c3c main + 20
14 libdyld.dylib 0x00007fff9a7de5fd start + 1
15 ??? 0x0000000000000001 0x0 + 1
----------------
我对这个错误感到困惑,因为该文件存在并且显然正在打开。错误消息完全具有误导性。我已经设法将错误定位到 while() 循环体中的 source.append(line); 行。
我偶然查看了string 类型的文档,发现它没有append 方法。然后,查看std.file的文档,发现有一个全局的append函数。那么错误消息是有道理的。
所以看起来编译器以某种方式设法在source 字符串对象上调用std.file.append(正如我们可以在异常调用堆栈中观察到的那样),从而导致了这个非常棘手的错误。这怎么可能?谁能解释一下这个问题?
如果我在这里遗漏了语言的一些基本点,请原谅我,我是初学者,但这似乎完全打破了类型安全的概念,不是吗?
【问题讨论】: