byKeyValue is not available at compilation-time right ?
jfondren
julian.fondren at gmail.com
Sun Jul 25 07:22:54 UTC 2021
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, 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.
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 here's something you can do:
```d
__gshared const dstring[][dstring] structureExchanges;
__gshared const dstring[dstring] exchangeStructures;
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.
More information about the Digitalmars-d-learn
mailing list