Associative arrays give compile error

Steven Schveighoffer schveiguy at yahoo.com
Tue Oct 5 07:14:27 PDT 2010


On Tue, 05 Oct 2010 09:00:13 -0400, Bob Cowdery <bob at bobcowdery.plus.com>  
wrote:

>  On 05/10/2010 13:45, Denis Koroskin wrote:

>> "static this" is called a static constructor and can be used for
>> classes and modules. The code in static constructor is guarantied to
>> be called before you use that class/module, it usually happens upon
>> thread initialization.
>>
>> The other solution is better though:
>>
>> enum A_RX_FILT = [                // just works
>>      "6K0": tuple(100.0, 6100.0),
>>      "2K4": tuple(300.0, 2700.0),
>>      "2K1": tuple(300.0, 2400.0),
>>      "1K0": tuple(300.0, 1300.0),
>>      "500": tuple(500.0, 1000.0),
>>      "250": tuple(600.0, 850.0),
>>      "100": tuple(700.0, 800.0)
>> ];
> I'm not totally understanding that. Why can enum compute that at compile
> time and the thing which it is, an associative array cannot. Is it to do
> with where these things live.

I'd be very wary of this solution.  Recently, enum has been shown to  
construct itself on every use.

So what I think is happening is every time you use A_RX_FILT, it's  
building a brand new AA (you can verify this by looking at the  
disassembly).

I'd recommend the static this solution in order to ensure you are not  
accidentally killing performance by just using that AA.

A while back, Don suggested that all literals should be considered  
immutable.  I agree with him, but Walter still doesn't.  If all literals  
are immutable, then they could be truly constructed at compile-time.

-Steve


More information about the Digitalmars-d-learn mailing list