byKeyValue is not available at compilation-time right ?

someone someone at somewhere.com
Sun Jul 25 18:03:05 UTC 2021


On Sunday, 25 July 2021 at 17:45:18 UTC, Adam D Ruppe wrote:
> On Sunday, 25 July 2021 at 17:29:46 UTC, someone wrote:
>> What is the proper syntax to use manifest-constants with 
>> associative arrays then ? The one you showed me ?
>
> You have the right syntax for that. What I'm saying is you 
> might not need the associative array at all. Why do you want to 
> use that specifically?

Good question.

I re-analyzed the code then.

Because I need code like the following:

```d
...
pstrExchangeID = structureExchanges[r"NYSE"d].ID;
...
```

... and/or (please, see the constructor):

```d
template classExchangeCustom(
    alias dstring lstrExchangeID,
    typeTickerCustom
    ) {

    public class classExchangeCustom : classExchange, 
interfaceExchangeCustom {

       /// (1) given exchange ID; eg: NYSE
       /// (2) given custom‐ticker type; eg: classTickerCustomNYSE

       private typeTickerCustom[dstring] pobjTickers;

       @safe @property public typeTickerCustom[dstring] tickers() 
{ return pobjTickers; }

       @safe this() {

          pudtLocation = 
structureExchanges[lstrExchangeID].location;
          pstrExchangeID = structureExchanges[lstrExchangeID].ID;
          pstrExchangeName = 
structureExchanges[lstrExchangeID].name;
          pstrCurrencyID = 
structureExchanges[lstrExchangeID].currencyID;

       }

       @safe public bool add(typeTickerCustom robjTickerCustom) {

          /// (1) reference to an already-created object for a 
custom‐ticker

          bool lbolAdded = false;

          if (robjTickerCustom ! is null) {

             lbolAdded = pobjTickers.require(
                robjTickerCustom.IDsymbolCommon,
                robjTickerCustom
                ) ! is null;

          }

          return lbolAdded;

       }

       @safe public bool add(const dstring lstrSymbolID) {

          /// (1) given symbol ID

          bool lbolAdded = false;

          typeTickerCustom lobjTickerCustom = new 
typeTickerCustom(lstrSymbolID);

          if (lobjTickerCustom ! is null) {

             lbolAdded = this.add(lobjTickerCustom);

          }

          return lbolAdded;

       }

    }

}
```

>> You say a normal foreach ... to be used at compilation-time 
>> ... huh ?
>
> Normal foreach is evaluated at compile time if it is looping 
> over an aliasseq tuple or if it is run in a compile time 
> context.
>
> Part of the magic of D is ordinary code might be compile time 
> or run time depending on how you use it.

Amazing :)

> But even static foreach I think can do the
>
> static foreach(k, v; your_assoc_array) {}
>
> in some cases.

I'll look into it.

All in all, and besides these minor issues, I am getting tons of 
totally unexpected flexibility with OOP on D. Some things I am 
doing right now like what I showed you, that probably to you all 
are nothing out-of-the-ordinary to me are ... fantastic features 
:)


More information about the Digitalmars-d-learn mailing list