general questions about static this() at module level

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 31 09:02:13 PDT 2016


On Monday, 31 October 2016 at 05:42:16 UTC, sarn wrote:
> On Monday, 31 October 2016 at 04:35:35 UTC, WhatMeWorry wrote:
>> [...]
>
> I've seen hacks to do the same thing in C++.  They're not 
> pretty, though.
>
>> [...]
>
> Class/struct static constructors are good for initialising 
> class/struct static data.  Module static constructors are good 
> for initialising module "static data" (i.e., globals).  They're 
> especially handy for initialising immutable global data (which 
> is the kind of global data D encourages).
>
> BTW, immutable data is shared between threads, so you should 
> use "shared static this" to initialise it.  Regular static 
> constructors are executed per thread.

Thanks! If you don't mind a follow up question, is this:

immutable uint maxSize = 128;

identical to this:

immutable uint maxSize;

static this()
{
     maxSize = 128;

}


More information about the Digitalmars-d-learn mailing list