On Thursday, 5 November 2020 at 21:18:52 UTC, Selim Ozel wrote:
auto records = rawtext.csvReader!struct_type1(';');

D is statically typed and `auto` means "deduce this type for me based on this one function's return value". It is not like JavaScript's `var` whose type may change.

If I'm not mistaken the `csvReader` function returns a range struct, and the full type is something long and unwieldy like `CsvReader!(struct_type1, cast(Malformed)1, string, dchar, string[])`. So just think of `records` as being that.

(You can tell what type it is at compilation with `pragma(msg, typeof(records).stringof)`.)

if(aControlCondition) {
records = rawtext.csvReader!struct_type2(';');

Here `csvReader!struct_type2(';')` returns a value of type `CsvReader!(struct_type2, ...)`, which is a different type from that of `records` (and they're not implicitly convertible). So the error message is right. If `auto` worked like `var` your code would work, but it doesn't.

You need two different variables and two different `foreach`es. For the same code to work on both types, the easy solution is templates. Perhaps make the `foreach` part after the reads a templated function that accepts any type passed to it?

Reply via email to