On Sunday, 25 July 2021 at 10:30:47 UTC, Adam D Ruppe wrote:
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....
}
?
What I was attempting to implement is a manifest constant as
described in "enum values that are not of an enum type" @
http://ddili.org/ders/d.en/enum.html specifically where it says:
- Such constants are rvalues and they are called manifest
constants.
- It is possible to create manifest constants of arrays and
associative arrays as well. However, as we will see later in the
Immutability chapter, enum arrays and associative arrays may have
hidden costs.
Since there is no example there the syntax that first occurred to
me was what you've already seen:
```d
enum structureLocations = [r"XXX"d : ...]
```
What is the proper syntax to use manifest-constants with
associative arrays then ? The one you showed me ?
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.
I am not following you :(
static foreach(
structureExchange sudtExchange;
structureExchanges.byKeyValue
) {
You can also try a normal foreach
foreach(k, v; structureExchanges) {
// use k and v
}
You say a normal foreach ... to be used at compilation-time ...
huh ?
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.
ACK. How should I've known LoL !
But the built-in k,v instead of byKeyValue might help anyway.