what is special about unittest constants

Ali Çehreli acehreli at yahoo.com
Mon Feb 18 09:22:11 PST 2013


On 02/18/2013 08:59 AM, Lubos Pintes wrote:
 > Yesterday I solved similar problem by using enum.
 > enum GameInfo[string] games=[
 > ...
 > ];

Be careful with that though: 'games' is a literal associative array, 
meaning that it will be used in the program as if it's copy-pasted in 
that location. It looks like there is a single associative array, but 
there is a new temporary created every time you use 'games':

struct GameInfo
{
     int i;
}

enum GameInfo[string] games = [ "one" : GameInfo(1) ];

void main()
{
     assert(&(games["one"]) != &(games["one"]));    // note: !=
}

As the two games are different, their elements are not at the same location.

This is one way to go:

struct GameInfo
{
     int i;
}

static immutable GameInfo[string] games;

static this()
{
     games = [ "one" : GameInfo(1) ];
}

void main()
{
     assert(&(games["one"]) == &(games["one"]));    // note ==
}

This time there is just one 'games'.

Ali



More information about the Digitalmars-d-learn mailing list