www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with circular imports of modules with static ctors an

reply Uranuz <neuranuz gmail.com> writes:
In my program I have error with circular imports of modules with 
static ctors. So I decided to move ctors in separate file and 
import it only from the 1st file. But problem is that in the 
first file I have immutables that should be initialized in shared 
static ctor. However doing it from another module's ctor gives 
compilation error: "Error: cannot modify immutable expression".
1. Is it a bug?
2. Could I solve this problem another way?

Example code:

module mod;

import mod_init;

static immutable string[string] aa;

...

--------------
module mod_init;

import mod;

shared static this
{
    aa = [ "a": "b", "c": "d" ]; //This gives compilation error
}

...
Apr 14 2016
parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Friday, 15 April 2016 at 05:35:24 UTC, Uranuz wrote:
 In my program I have error with circular imports of modules 
 with static ctors. So I decided to move ctors in separate file 
 and import it only from the 1st file. But problem is that in 
 the first file I have immutables that should be initialized in 
 shared static ctor. However doing it from another module's ctor 
 gives compilation error: "Error: cannot modify immutable 
 expression".
 1. Is it a bug?
Not really. The rules are there to avoid cyclic initialization dependencies, but they are a bit coarse at the moment.
 2. Could I solve this problem another way?
You could put the immutable globals in the the same module as the shared ctors, and publicly import them in the "real" modules they are intended to be in. You might have to play around with `package` protection if you want to make them invisible in the helper module, too.
Apr 15 2016