On Thursday, 5 August 2021 at 15:26:33 UTC, H. S. Teoh wrote:
On Thu, Aug 05, 2021 at 03:09:13PM +0000, someone via
Digitalmars-d-learn wrote:
On Thursday, 5 August 2021 at 10:28:00 UTC, Steven
Schveighoffer wrote:
> H.S. Teoh, I know you know better than this ;) None of this
> is necessary, you just need `rtValue` for both runtime and
> CTFE (and compile time parameters)!
Haha, I haven't used this particular feature of D recently, so
probably my memory is failing me. ;-)
> Now, the original question is about *associative arrays*,
> which are a different animal. Those, you actually have to
> initialize using a static constructor, and does indeed need
> both an enum and a static immutable, as CTFE currently does
> not understand runtime AAs. This is a huge issue since you
> do need silly things like the `if(__ctfe)` statement you
> wrote, and keep an enum handy for those cases which is
> identical to the static immutable. We really need to fix
> this.
When you say "We really need to fix this" you mean that
*eventually* associative-arrays will be available at
compile-time ?
[...]
AA's are already available at compile-time. You can define
them in CTFE and pass them around as template arguments.
What doesn't work is initializing global static immutable AA's
with literals. Currently, you need this workaround:
struct Data { /* whatever you want to store here */ }
static immutable Data[string] aa;
shared static this() {
aa = [
"abc": Data(...),
"def": Data(...),
// ... etc.
];
}
Unfortunately, this also means you can't access the value of
`aa` at compile-time. So you need a separate enum in order to
access AA values at compile-time.
Full runnable example:
---------------
enum ctValue = [
"abc": 123,
"def": 456,
];
static immutable int[string] rtValue;
shared static this() {
rtValue = ctValue;
}
// Compile-time operations
enum x = ctValue["abc"];
enum y = ctValue["def"];
static assert(x == 123 && y == 456);
// Runtime operations
void main() {
assert(rtValue["abc"] == 123);
assert(rtValue["def"] == 456);
}
---------------
T
So if we are talking AA-arrays at compile-time only there should
be nothing wrong with the following code ... right ?
private struct structureLocation {
dstring countryID = null;
dstring countryName = null;
dstring city = null;
dstring TZ = null;
}
private enum pudtLocations = [
r"BUE"d : structureLocation(r"arg"d, r"Buenos Aires"d,
r"ART"d),
r"GRU"d : structureLocation(r"bra"d, r"São Paulo"d, r"BRT"d),
r"HHN"d : structureLocation(r"deu"d, r"Frankfurt am Main"d,
r"CET"d),
r"LHR"d : structureLocation(r"gbr"d, r"London"d, r"UTC"d),
r"NYC"d : structureLocation(r"usa"d, r"New York"d, r"EST"d)
];
private struct structureExchange {
structureLocation location;
dstring ID = null;
dstring name = null;
dstring currencyID = null;
}
private enum dstring pstrExchangeIDB3 = r"B3"d;
private enum dstring pstrExchangeIDBCBA = r"BCBA"d;
private enum dstring pstrExchangeIDLSE = r"LSE"d;
private enum dstring pstrExchangeIDNASDAQ = r"NASDAQ"d;
private enum dstring pstrExchangeIDNYSE = r"NYSE"d;
private enum dstring pstrExchangeIDXETRA = r"XETRA"d;
public enum gudtExchanges = [
pstrExchangeIDB3 :
structureExchange(pudtLocations[r"GRU"d], pstrExchangeIDB3 ,
r"B3 formerly Bolsa de Valores de São Paulo (aka BOVESPA)"d,
r"BRL"d),
pstrExchangeIDBCBA :
structureExchange(pudtLocations[r"BUE"d], pstrExchangeIDBCBA ,
r"Bolsa de Comercio de Buenos Aires"d, r"ARS"d),
pstrExchangeIDLSE :
structureExchange(pudtLocations[r"LHR"d], pstrExchangeIDLSE ,
r"London Stock Exchange"d, r"GBP"d),
pstrExchangeIDNASDAQ :
structureExchange(pudtLocations[r"NYC"d], pstrExchangeIDNASDAQ,
r"National Association of Securities Dealers Automated
Quotations"d, r"USD"d),
pstrExchangeIDNYSE :
structureExchange(pudtLocations[r"NYC"d], pstrExchangeIDNYSE ,
r"New York Stock Exchange"d, r"USD"d),
pstrExchangeIDXETRA :
structureExchange(pudtLocations[r"HHN"d], pstrExchangeIDXETRA ,
r"Deutsche Börse"d, r"EUR"d)
]; /// byKeyValue is not available at compile‐time; hence the
redundancy of IDs
/*public enum gudtExchanges = [
pstrExchangeIDB3 :
structureExchange(pudtLocations[r"GRU"d], r"B3 formerly Bolsa de
Valores de São Paulo (aka BOVESPA)"d, r"BRL"d),
pstrExchangeIDBCBA :
structureExchange(pudtLocations[r"BUE"d], r"Bolsa de Comercio de
Buenos Aires"d, r"ARS"d),
pstrExchangeIDLSE :
structureExchange(pudtLocations[r"LHR"d], r"London Stock
Exchange"d, r"GBP"d),
pstrExchangeIDNASDAQ :
structureExchange(pudtLocations[r"NYC"d], r"National Association
of Securities Dealers Automated Quotations"d, r"USD"d),
pstrExchangeIDNYSE :
structureExchange(pudtLocations[r"NYC"d], r"New York Stock
Exchange"d, r"USD"d),
pstrExchangeIDXETRA :
structureExchange(pudtLocations[r"HHN"d], r"Deutsche Börse"d,
r"EUR"d)
];*/ /// byKeyValue eventually becomes available
...
static foreach (
structureExchange sudtExchange;
gudtExchanges
) {
mixin(
... sudtExchange.ID ...
... sudtExchange.name ...
... sudtExchange.CurrencyID ...
... sudtExchange.location.countryID ...
... sudtExchange.location.countryName ...
... sudtExchange.location.city ...
... sudtExchange.location.TZ ...
);
}