best/proper way to declare constants ?

someone someone at somewhere.com
Thu Aug 5 03:50:18 UTC 2021


On Thursday, 5 August 2021 at 03:20:17 UTC, H. S. Teoh wrote:
> On Thu, Aug 05, 2021 at 01:39:42AM +0000, someone via 
> Digitalmars-d-learn wrote: [...]
>> What happens in the following case ?
>> 
>> public immutable enum gudtLocations = [
>>    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)
>>    ];
>> 
>> This is something that I also need at compilation time.
> [...]
>
> If you need a constant array value both at compile-time and 
> runtime, one way to do it is to declare an enum that is used 
> only by compile-time code, and the same enum is used once to 
> declare the runtime static immutable.
>
> Example:
>
> 	enum ctValue = [ "my", "data", "here", ... ];
>
> 	// Initialize this once with ctValue.
> 	static immutable string[] rtValue = ctValue;
>
> 	if (ctfe) {
> 		// Compile-time: use ctValue
> 		foreach (value; ctValue) {
> 			...
> 		}
> 	} else {
> 		// Runtime: use rtValue instead
> 		foreach (value; rtValue) {
> 			...
> 		}
> 	}

Nice and fine.

Problem is that in your example ctValue is int by default; e: 
my=0, data=1, etc

And what I need at compiled time are strings to build/name 
classes and the like.

I need a compile-time enum (or whatever) that gets me "NYSE" 
"NASDAQ" etc AND that I can use with static foreach {}

My code is far from right but it actually does the job right now:

public enum gudtLocations = [
    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)
    ];

public enum gudtExchanges = [
    r"B3"d     : structureExchange(gudtLocations[r"GRU"d], r"B3"d, 
r"B3 formerly Bolsa de Valores de São Paulo (aka BOVESPA)"d, 
r"BRL"d),
    r"BCBA"d   : structureExchange(gudtLocations[r"BUE"d], 
r"BCBA"d, r"Bolsa de Comercio de Buenos Aires"d, r"ARS"d),
    r"LSE"d    : structureExchange(gudtLocations[r"LHR"d], 
r"LSE"d, r"London Stock Exchange"d, r"GBP"d),
    r"NASDAQ"d : structureExchange(gudtLocations[r"NYC"d], 
r"NASDAQ"d, r"National Association of Securities Dealers 
Automated Quotations"d, r"USD"d),
    r"NYSE"d   : structureExchange(gudtLocations[r"NYC"d], 
r"NYSE"d, r"New York Stock Exchange"d, r"USD"d),
    r"XETRA"d  : structureExchange(gudtLocations[r"HHN"d], 
r"XETRA"d, r"Deutsche Börse"d, r"EUR"d)
    ]; /// byKeyValue is not available at compile‐time; hence the 
redundancy of IDs

And of course now I fully understand why it is not optimal at all.

> Just be sure you don't use ctValue during runtime, otherwise it 
> will incur an allocation per use.
>
>
> T




More information about the Digitalmars-d-learn mailing list