constructing module level immutable variables at runtime?

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Feb 22 07:09:11 PST 2016


On 2/22/16 9:58 AM, Danni Coy via Digitalmars-d wrote:
> That worked however it seems that you can only do the assignments
> within the shared static this() and not from functions called by the
> initialiser which might change how I do things
>

This is true. The compiler wants to be super-sure that you are only 
setting it once.

However, this can be alleviated by changing how you do things. Factor 
out the code that sets it, and have it instead return a value. Then you 
can do this:

shared static this() {
    num_triggers = myCrazyFunction();
}

Of course, myCrazyFunction must not depend on any other static 
variables, as those might not be initialized.

startup initialization can be very tricky.

-Steve


More information about the Digitalmars-d mailing list