I wanted a lightweight and simpler CSV decoder. I won't post the whole thing, but basically you instantiate one as:

struct Whatever {
   ...
}
...
    f = File("path.csv", "r");
    auto c = CSVreader!Whatever(f);
    foreach (rec; c) { ...

CSVreader is, of course, templated:

struct CSVreader(T) {
    ...
}

and the innermost bit of CSVreader is:

    auto t = T();
    foreach (i, ref val; t.tupleof) {
        static if (is(typeof(val) == int)) {
            val = this.get_int();
        } else {
            val = this.get_str();
        }
    }
    return t;

So you cue off the type of the struct field, and decode the next CSV field, and put the value into the new struct.

Is there a cleaner way to do this? This _does_ work, and gives me very compact code.

Reply via email to