April 15, 2016 Problem with circular imports of modules with static ctors an immutable variables | ||||
---|---|---|---|---|
| ||||
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 } ... |
April 15, 2016 Re: Problem with circular imports of modules with static ctors an immutable variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uranuz | 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. |
Copyright © 1999-2021 by the D Language Foundation