Associative arrays give compile error

Bob Cowdery bob at bobcowdery.plus.com
Tue Oct 5 06:00:13 PDT 2010


 On 05/10/2010 13:45, Denis Koroskin wrote:
> On Tue, 05 Oct 2010 16:32:14 +0400, Bob Cowdery
> <bob at bobcowdery.plus.com> wrote:
>
>>  On 05/10/2010 13:05, Denis Koroskin wrote:
>>> On Tue, 05 Oct 2010 15:53:55 +0400, Denis Koroskin <2korden at gmail.com>
>>> wrote:
>>>
>>>> On Tue, 05 Oct 2010 15:40:39 +0400, Bob Cowdery
>>>> <bob at bobcowdery.plus.com> wrote:
>>>>
>>>>>  On 05/10/2010 12:13, Denis Koroskin wrote:
>>>>>> On Tue, 05 Oct 2010 15:08:39 +0400, Bob Cowdery
>>>>>> <bob at bobcowdery.plus.com> wrote:
>>>>>>
>>>>>>>  On 05/10/2010 12:04, Denis Koroskin wrote:
>>>>>>>> On Tue, 05 Oct 2010 14:57:22 +0400, Bob Cowdery
>>>>>>>> <bob at bobcowdery.plus.com> wrote:
>>>>>>>>
>>>>>>>>>  On 05/10/2010 11:45, Denis Koroskin wrote:
>>>>>>>>>> On Tue, 05 Oct 2010 14:23:47 +0400, Bob Cowdery
>>>>>>>>>> <bob at bobcowdery.plus.com> wrote:
>>>>>>>>>>
>>>>>>>>>>>  I can't seem to get any sense out of associative arrays. Even
>>>>>>>>>>> the
>>>>>>>>>>> simplest definition won't compile so I must be doing something
>>>>>>>>>>> daft.
>>>>>>>>>>>
>>>>>>>>>>> int[string] aa = ["hello":42];
>>>>>>>>>>>
>>>>>>>>>>> Error: non-constant expression ["hello":42]
>>>>>>>>>>>
>>>>>>>>>>> What exactly is not constant about this. The example is
>>>>>>>>>>> straight
>>>>>>>>>>> out the
>>>>>>>>>>> book. Using D 2.0.
>>>>>>>>>>>
>>>>>>>>>>> bob
>>>>>>>>>>
>>>>>>>>>> What exactly compiler version are you using (run dmd with no
>>>>>>>>>> args)?
>>>>>>>>>> Works perfectly fine here (dmd2.049).
>>>>>>>>>
>>>>>>>>> It says 2.049. How odd. I've got a fair amount of code and
>>>>>>>>> everything
>>>>>>>>> else compiles fine.
>>>>>>>>
>>>>>>>> Can you please post complete code snippet that fails to compile?
>>>>>>>>
>>>>>>>> Here is the code I used to test:
>>>>>>>>
>>>>>>>> module aa;
>>>>>>>>
>>>>>>>> import std.stdio;
>>>>>>>>
>>>>>>>> void main()
>>>>>>>> {
>>>>>>>>     int[string] aa = ["hello":42];
>>>>>>>>     writeln(aa["hello"]);
>>>>>>>> }
>>>>>>>>
>>>>>>>> # dmd -run aa.d
>>>>>>>
>>>>>>> Ah! It's some other code below it that is not giving an error but
>>>>>>> causing the error above. So the compiler is getting confused. What
>>>>>>> I was
>>>>>>> actually trying to do was create an associative array with a
>>>>>>> string as a
>>>>>>> key and a Tuple as the value. Now
>>>>>>>
>>>>>>> auto aa = [
>>>>>>>     "some string": (100.0, 6100.0)
>>>>>>> ]
>>>>>>>
>>>>>>> compiles but is clearly wrong and gives rise to other errors.  Does
>>>>>>> anyone know the correct way to define this and then access the
>>>>>>> tuple.
>>>>>>
>>>>>> import std.stdio;
>>>>>> import std.typecons;
>>>>>>
>>>>>> void main()
>>>>>> {
>>>>>>     auto aa = ["hello": tuple(100.0, 6100.0)];
>>>>>>     auto result = aa["hello"];
>>>>>>
>>>>>>     writeln(result.field[0], " ", result._1); // primary and
>>>>>> alternative way
>>>>>> }
>>>>>
>>>>> Thanks. I've established that works for me and also that the actual
>>>>> array I'm using also works in the test program but it won't
>>>>> compile in
>>>>> the real program. I've commented everything else out of the file and
>>>>> just left...
>>>>>
>>>>> import std.typecons;
>>>>>
>>>>> auto A_RX_FILT = [
>>>>>     "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)
>>>>> ];
>>>>>
>>>>
>>>> You are trying to declare global variable and initialize at in
>>>> compile time. As far as I know, you can't initialize AA at compile
>>>> time atm (this might be implemented in future though).
>>>>
>>>> As such, I'd recommend against using global variables (try moving it
>>>> to some class or something). Anyway, you need to initialize it at
>>>> some point, either manually:
>>>>
>>>> Tuple!(double,double)[string] A_RX_FILT;
>>>>
>>>> void init()
>>>> {
>>>>      A_RX_FILT = [
>>>>     "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)
>>>>      ];
>>>> }
>>>>
>>>> or automatically at thread startup:
>>>>
>>>> static this()
>>>> {
>>>>      init();
>>>> }
>>>>
>>>> Hope that helps.
>>>
>>> See my other reply for a better solution.
>>
>> Thanks very much. It compiles now. The reason I thought it was an issue
>> was because sometime it did compile a global associative array. I need
>> to do some homework on what 'this' does. It's clearly a powerful concept
>> and has wider application than class constructors.
>>
>
> "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.


More information about the Digitalmars-d-learn mailing list