Problem with circular imports of modules with static ctors an immutable variables

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 15 01:17:18 PDT 2016


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.


More information about the Digitalmars-d-learn mailing list