On Wednesday, 9 February 2022 at 08:12:52 UTC, Vindex wrote:
Will the loop (foreach) run at compile time? How can I make it work at compile time?

```
import std.csv, std.stdio;

alias Record = Tuple!(string, string, string);

immutable string[][] table;

shared static this() {
    string csvText = import("file.csv");
    foreach (record; csvReader!Record(csvText)) {
        table ~= [record[0], record[1], record[2]];
    }
}

void main() {
    writeln(table):
}
```

I would do this.

```
import std;

alias Record = Tuple!(string, string, string);

static immutable string[][] table = () {
    string[][] table;
    string csvText = import("file.csv");

    foreach (record; csvReader!Record(csvText)) {
        table ~= [record[0], record[1], record[2]];
    }

    return table;
}();

pragma(msg, table);  // Available at compile-time

void main() {
    writeln(table);
}
```

And then `-J{path}` to tell the compiler where to find `file.csv`.

```
dmd -J. csv.d
```

Reply via email to