I haven't been able to reduce this far enough to know for sure whether this is a DMD or Phobos bug, but the following ternary operator expression should work:
import std.stdio;
void main() {
bool foo;
auto lines = (foo ? File("test.d") : File("test.d")).byLine();
writeln(lines.empty); // true
auto handle = File("test.d");
lines = handle.byLine();
writeln(lines.empty); // false
}
Comment #1 by lovelydear — 2012-04-23T03:58:17Z
This is not a bug. The test actually works if the file "test.d" exists beforehand.
If it doesn't exist, one must open the file in write mode. By default, the file is open in read mode.
This works as intended.
import std.stdio;
void main() {
bool foo;
auto lines = (foo ? File("test.d", "w") : File("test.d", "w")).byLine();
writeln(lines.empty); // true
auto handle = File("test.d");
lines = handle.byLine();
writeln(lines.empty); // false
}
Comment #2 by github-bugzilla — 2014-03-15T20:40:25Z