www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initialising invariant associative array

reply David Ferenczi <raggae ferenczi.net> writes:
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
Jun 18 2008
parent reply Gide Nwawudu <gide btinternet.com> writes:
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
Jun 18 2008
next sibling parent David Ferenczi <raggae ferenczi.net> writes:
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
Jun 18 2008
prev sibling parent reply David Ferenczi <raggae ferenczi.net> writes:
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
Jun 19 2008
parent reply Gide Nwawudu <gide btinternet.com> writes:
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
Jun 24 2008
parent David Ferenczi <raggae ferenczi.net> writes:
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
Jun 24 2008