byKeyValue is not available at compilation-time right ?

Adam D Ruppe destructionator at gmail.com
Sun Jul 25 10:30:47 UTC 2021


On Sunday, 25 July 2021 at 05:10:32 UTC, someone wrote:
> As you can see in the following code I cannot avoid to type the 
> 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),

Coudln't you instead just do like

enum structureLoctations = {
    BUE = typeLocation......,
    GRU = typeLiocation....
}


?


Then you can build a runtime hash map if you need it in a static 
constructor or use a binary search switch to convert from strings 
and look up the id from reflection. It depends on the usage.


> static foreach(
>    structureExchange sudtExchange;
>    structureExchanges.byKeyValue
>    ) {

You can also try a normal foreach


foreach(k, v; structureExchanges) {
     // use k and v
}


static foreach might work too but assocative arrays are weird 
beasts that needs to exist all at run time or all at compile 
time; they cannot cross the barrier and use one item for both.

But the built-in k,v instead of byKeyValue might help anyway.


More information about the Digitalmars-d-learn mailing list