On Wednesday, 9 February 2022 at 10:01:15 UTC, Anonymouse wrote:
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
```
Is it guaranteed that the value is initialized at compiletime
however?
Something being available at compiletime isn't the same as being
initialized at compiletime only.
If it's guaranteed then that's indeed the best solution.