Filling an array at compile time

Vindex tech.vindex at gmail.com
Wed Feb 9 12:54:02 UTC 2022


On Wednesday, 9 February 2022 at 10:01:15 UTC, Anonymouse wrote:
> On Wednesday, 9 February 2022 at 08:12:52 UTC, Vindex wrote:
>> [...]
>
> 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
> ```

Thanks! It does indeed work at compile time.


More information about the Digitalmars-d-learn mailing list