I am learning D and trying to split strings:
import std.stdio;
import std.string;
auto file = File(path, "r");
foreach (line; file.byLine) {
string[] parts = split(line);
This fails to compile with:
Error: cannot implicitly convert expression (split(line)) of type char[][] to string[]
This works:
auto file = File(path, "r");
foreach (line; file.byLine) {
char[][] parts = split(line);
But why do I have to use a char[][]
? As far as I understand the documentation, it says that split
returns a string[]
, which I would prefer.
Use split(line.idup)
;
split
is a template function, the return type depends on its argument. file.byLine.front
returns a char[]
which is also reused for performance reasons. So if you need the parts after the current loop iteration you have to do a dup
or idup
, whatever you need.