byKeyValue is not available at compilation-time right ?

someone someone at somewhere.com
Sun Jul 25 17:38:23 UTC 2021


On Sunday, 25 July 2021 at 07:22:54 UTC, jfondren wrote:
> On Sunday, 25 July 2021 at 05:10:32 UTC, someone wrote:
>> /// implementation: however, would it be possible to 
>> dynamically‐load the following enums from a file at 
>> compilation‐time ?
>>
>> public immutable enum structureLocations = [
>>    r"BUE"d : typeLocation(r"arg"d, r"Buenos Aires"d, r"ART"d),
>>    r"GRU"d : typeLocation(r"bra"d, r"São Paulo"d, r"BRT"d),
>>    r"HHN"d : typeLocation(r"deu"d, r"Frankfurt am Main"d, 
>> r"CET"d),
>>    r"LHR"d : typeLocation(r"gbr"d, r"London"d, r"UTC"d),
>>    r"NYC"d : typeLocation(r"usa"d, r"New York"d, r"EST"d)
>>    ];
>
> ```
> Error: `_aaRange` cannot be interpreted at compile time, 
> because it has no available source code
> ```
>
> Yep, seems that's not available.
>
> What you're doing with `immutable enum structureLocations` is 
> creating a manifest constant

Right.

> i.e. your AA initialization is copy-pasted into each use of it, 
> which means your program is rebuilding this AA at runtime every 
> time it comes up. You probably got to this point as a bare 
> `immutable structureLocations` errored out to the non-constant 
> expression.

Sounds bad, inefficient at least :(

I refactored this little chunk of code many times and I thought 
this was the best one that I came across.

> You should be able to use a shared static module initializer to 
> initialize an immutable AA, 
> https://dlang.org/spec/module.html#staticorder , but although 
> I've seen examples of simple string[string] initialization, it 
> seems to be very hard to get more complex AAs past the 
> std.array/std.exception tools.

So I am seeking free trouble then :(

> So here's something you can do:
>
> ```d
> __gshared const dstring[][dstring] structureExchanges;
> __gshared const dstring[dstring] exchangeStructures;

ahhh, the __gshared attribute, I remember it while trying to 
understand the differences for all the attributes: multi‐tasking 
related: to share (non‐immutable global declarations) across all 
threads vs local‐storage (default) ... right ?

> shared static this() {
>     import std.string, std.algorithm;
>
>     dstring[][dstring] exchanges;
>     // could easily load this from a file
>     "   B3: B3 formerly Bolsa de Valores de São Paulo (aka 
> BOVESPA)
>         BCBA: Bolsa de Comercio de Buenos Aires
>         LSE: London Stock Exchange
>         NASDAQ: National Association of Securities Dealers 
> Automated Quotations
>         NYSE: New York Stock Exchange
>         XETRA: Deutsche Börse
>     "d.strip.splitLines.map!strip.each!((dstring exch) {
>         const pair = exch.split(": ");
>         exchanges[pair[0]] = [pair[1], "some other values"d];
>     });
>     structureExchanges = exchanges;
>
>     dstring[dstring] structures;
>     foreach (k, v; exchanges)
>         structures[v[0]] = k;
>     exchangeStructures = structures;
> }
> ```
>
> std.array.assocArray might also come in handy.

I suppose I should rethink this matter once again then; I do not 
want to introduce things that make debugging harder than needed.

I'll try to analyze and to implement what you showed me and see 
how it unrolls.


More information about the Digitalmars-d-learn mailing list