Thread overview
Initialising invariant associative array
Jun 18, 2008
David Ferenczi
Jun 18, 2008
Gide Nwawudu
Jun 18, 2008
David Ferenczi
Jun 19, 2008
David Ferenczi
Jun 24, 2008
Gide Nwawudu
Jun 24, 2008
David Ferenczi
June 18, 2008
The following code gives the following compilation error:

src/test/test.d(13): Error: non-constant expression ["s1":cast(FOO
9223372036854775808LU,"s2":cast(FOO)4611686018427387904LU]
src/test/test.d(14): Error: non-constant expression [cast(FOO
9223372036854775808LU:"s1",cast(FOO)4611686018427387904LU:"s2"]


--------------------------8<------------------------------------
import std.stdint: uint_fast64_t;

class A
{
    typedef uint_fast64_t FOO;

    static invariant FOO fooValue1 = cast(FOO) 0x8000000000000000LU;
    static invariant FOO fooValue2 = cast(FOO) 0x4000000000000000LU;

    static invariant FOO[string] fooArray =
["s1":fooValue1, "s2":fooValue2];
    static invariant string[FOO] strArray = [fooValue1:"s1",
fooValue2:"s2"];
}

int main(string[] args)
{
    A a;

    return 0;
}
--------------------------8<------------------------------------

Can somebody explain me why?

Thanks,
David
June 18, 2008
On Wed, 18 Jun 2008 10:21:05 +0200, David Ferenczi <raggae@ferenczi.net> wrote:

>The following code gives the following compilation error:
>
>src/test/test.d(13): Error: non-constant expression ["s1":cast(FOO
>9223372036854775808LU,"s2":cast(FOO)4611686018427387904LU]
>src/test/test.d(14): Error: non-constant expression [cast(FOO
>9223372036854775808LU:"s1",cast(FOO)4611686018427387904LU:"s2"]
>
>
>--------------------------8<------------------------------------
>import std.stdint: uint_fast64_t;
>
>class A
>{
>    typedef uint_fast64_t FOO;
>
>    static invariant FOO fooValue1 = cast(FOO) 0x8000000000000000LU;
>    static invariant FOO fooValue2 = cast(FOO) 0x4000000000000000LU;
>
>    static invariant FOO[string] fooArray =
>["s1":fooValue1, "s2":fooValue2];
>    static invariant string[FOO] strArray = [fooValue1:"s1",
>fooValue2:"s2"];
>}
>
>int main(string[] args)
>{
>    A a;
>
>    return 0;
>}
>--------------------------8<------------------------------------
>
>Can somebody explain me why?
>
>Thanks,
>David

D uses 'static this' to initialise static members. This is done to remove order of evaluation issues. The docs contain more info on static constructors, see http://www.digitalmars.com/d/2.0/class.html section 'Static Constructors'.

import std.stdint: uint_fast64_t;

class A
{
    typedef uint_fast64_t FOO;

    static invariant FOO fooValue1;
    static invariant FOO fooValue2;

    static invariant FOO[string] fooArray;
    static invariant string[FOO] strArray;

    static this() {
        fooValue1 = cast(FOO) 0x8000000000000000LU;
        fooValue2 = cast(FOO) 0x4000000000000000LU;

        fooArray = ["s1":fooValue1, "s2":fooValue2];
        strArray = [fooValue1:"s1", fooValue2:"s2"];
    }
}

int main(string[] args)
{
    A a;

    return 0;
}


Gide
June 18, 2008
Gide Nwawudu wrote:

> On Wed, 18 Jun 2008 10:21:05 +0200, David Ferenczi <raggae@ferenczi.net> wrote:
> 
>>The following code gives the following compilation error:
>>
>>src/test/test.d(13): Error: non-constant expression ["s1":cast(FOO
>>9223372036854775808LU,"s2":cast(FOO)4611686018427387904LU]
>>src/test/test.d(14): Error: non-constant expression [cast(FOO
>>9223372036854775808LU:"s1",cast(FOO)4611686018427387904LU:"s2"]
>>
>>
>>--------------------------8<------------------------------------
>>import std.stdint: uint_fast64_t;
>>
>>class A
>>{
>>    typedef uint_fast64_t FOO;
>>
>>    static invariant FOO fooValue1 = cast(FOO) 0x8000000000000000LU;
>>    static invariant FOO fooValue2 = cast(FOO) 0x4000000000000000LU;
>>
>>    static invariant FOO[string] fooArray =
>>["s1":fooValue1, "s2":fooValue2];
>>    static invariant string[FOO] strArray = [fooValue1:"s1",
>>fooValue2:"s2"];
>>}
>>
>>int main(string[] args)
>>{
>>    A a;
>>
>>    return 0;
>>}
>>--------------------------8<------------------------------------
>>
>>Can somebody explain me why?
>>
>>Thanks,
>>David
> 
> D uses 'static this' to initialise static members. This is done to remove order of evaluation issues. The docs contain more info on static constructors, see http://www.digitalmars.com/d/2.0/class.html section 'Static Constructors'.
> 
> import std.stdint: uint_fast64_t;
> 
> class A
> {
>     typedef uint_fast64_t FOO;
> 
>     static invariant FOO fooValue1;
>     static invariant FOO fooValue2;
> 
>     static invariant FOO[string] fooArray;
>     static invariant string[FOO] strArray;
> 
>     static this() {
>         fooValue1 = cast(FOO) 0x8000000000000000LU;
>         fooValue2 = cast(FOO) 0x4000000000000000LU;
> 
>         fooArray = ["s1":fooValue1, "s2":fooValue2];
>         strArray = [fooValue1:"s1", fooValue2:"s2"];
>     }
> }
> 
> int main(string[] args)
> {
>     A a;
> 
>     return 0;
> }
> 
> 
> Gide

Thank you very much for your help. Initialising in the static constructor works well.

Thanks,
David
June 19, 2008
The code compiles, but it generates a segmentation fault. The problem is caused by the array initialisation:

>         fooArray = ["s1":fooValue1, "s2":fooValue2];
>         strArray = [fooValue1:"s1", fooValue2:"s2"];

David



June 24, 2008
On Thu, 19 Jun 2008 10:40:48 +0200, David Ferenczi <raggae@ferenczi.net> wrote:

>The code compiles, but it generates a segmentation fault. The problem is caused by the array initialisation:
>
>>         fooArray = ["s1":fooValue1, "s2":fooValue2];
>>         strArray = [fooValue1:"s1", fooValue2:"s2"];
>
>David
>
>

I think this is a bug, see

http://d.puremagic.com/issues/show_bug.cgi?id=1727

Gide
June 24, 2008
Gide Nwawudu wrote:

> On Thu, 19 Jun 2008 10:40:48 +0200, David Ferenczi <raggae@ferenczi.net> wrote:
> 
>>The code compiles, but it generates a segmentation fault. The problem is caused by the array initialisation:
>>
>>>         fooArray = ["s1":fooValue1, "s2":fooValue2];
>>>         strArray = [fooValue1:"s1", fooValue2:"s2"];
>>
>>David
>>
>>
> 
> I think this is a bug, see
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=1727
> 
> Gide

I haven't found this former issue, so I also filed a bug:

http://d.puremagic.com/issues/show_bug.cgi?id=2158

It may be then a duplicate.

David