Fixing cyclic import static construction problems

Timon Gehr timon.gehr at gmx.ch
Thu Nov 29 14:24:15 PST 2012


On 11/29/2012 10:45 PM, deadalnix wrote:
> On Thursday, 29 November 2012 at 21:43:30 UTC, Jonathan M Davis wrote:
>> On Thursday, November 29, 2012 21:08:58 Jacob Carlborg wrote:
>>> BTW, how does Java handle this? And C# if it has something similar.
>>
>> They just let you blow your foot off. All static variables can be
>> directly
>> initialized at runtime, so it's easy to use variables before they're
>> actually
>> initialized. I don't know how they decide what order to run static
>> constructors in, but AFAIK, it never worries about circular dependencies.
>> We're only running into this problem beacuse we're trying to provide
>> higher
>> safety and better guarantees with regards to when and how variables are
>> initialized.
>>
>> - Jonathan M Davis
>
> Java have static block to pre initialize stuff before it is used. But
> I'm not sure how they react in case of cyclic dependancies.

In Java and C# static constructors are run whenever the class is first 
referenced. This means that almost any statement in Java or C# may cause 
arbitrary side-effecting code to run. Circular dependencies are not 
considered at all. If two static constructors depend on each other's 
statically initialized parts, at least one of them will see 
uninitialized data.


More information about the Digitalmars-d mailing list