How do I use CTFE to generate an immutable associative array at compile time?
Chad Joan via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Feb 21 14:34:57 PST 2017
On Tuesday, 21 February 2017 at 22:26:01 UTC, Chad Joan wrote:
> On Tuesday, 21 February 2017 at 21:58:07 UTC, Stefan Koch wrote:
>> On Tuesday, 21 February 2017 at 21:53:23 UTC, Chad Joan wrote:
>>> Hello all,
>>>
>>> I'm trying to make this work:
>>>
>>> [...]
>>
>> You cannot create AA's at ctfe and carry them over to runtime
>> use.
>> You'd have to use a costum dictionary-type.
>> I think the vibe.d project has one you could use.
>
> I wondered if this might be the case.
>
> Well, I won't worry about it then. I'll have to find another
> way around.
>
> Thanks a bunch!
For the sake of the rest of the internet, here is what I'm
probably going to stick with:
---
pure string[string] parseTwoColumnCsv(string inputCsv)
{
import std.csv;
import std.typecons;
string[string] result;
foreach ( record; csvReader!(Tuple!(string,string))(inputCsv) )
result[record[0]] = record[1];
return result;
}
immutable string[string] dataLookup;
static this()
{
dataLookup = parseTwoColumnCsv(import("some_data.csv"));
}
void main()
{
import std.stdio;
writefln("dataLookup = %s", dataLookup);
}
---
In this case the AA isn't actually coded into the executable; but
at least the configuration from some_data.csv will be in the
executable as a string. The program will construct the AA at
startup. It's not as "cool", but it should get the job done.
HTH.
More information about the Digitalmars-d-learn
mailing list